Escolar Documentos
Profissional Documentos
Cultura Documentos
http://www.panzerbasics.com/index_files/97-excel/basic-macros.htm
The codes for these basic macros came from all over the internet or have been found by myself.
Since these are very common and basic, I did not bother mentioning sources. Should someone
recognise work of his own here and needs a name to be mentioned or the code removed, please
contact me.
The macros shown here are my own selection and should do to get started or help you out on
basic projects. Use your imagination to adjust them to fit in your project, or search for more info
on the internet.
The codes here have been tried and verified to work on my Excel 2007 Version. Please note that
it is often possible to reach the same goal in a different way.
Macros usually start with the line: "Sub Name()", where Name can be replaced by any name you
assign to it. Macros end with the line "End Sub".
For clarity and ease of use testing different ones, I left these lines out unless they differ for some
reason.
If in the above, "Sub" is replaced with "Function", the code works the same, but the macro does
not appear in the list of available macros. The downside of this method is that the function will
appear in the functions list.(Generally used for calculations and returning a value.) I sometimes
prefer this to keep overview in the macros list. If "Private" is used before "Sub" or "Function", it
can only be called from the same module, whilst also not being visible in the list.
Beware that the code shown on this page is made to look correct on your screen. Using
copy/paste may result in not working code because some characters have been replaced because
of the xhtml code
For example: a shown "&" on this page will show as "&" when copied.
Description Code Remarks
myRow = ActiveCell.Row
Active Cell,
myCol = ActiveCell.Column Shows Active Cell position.
Position
MsgBox myRow & "," & myCol
Selection.End(xlToLeft).Select The top one takes the active
Active Cell, cell to the far left, but not if
Selection to far OR there is an empty cell in
left between. To counter that the
Range("A" & ActiveCell.Row).Select bottom one works better.
With ActiveWindow
Active Cell in Scrolls to place the ActiveCell
.ScrollColumn = ActiveCell.Column
top left of be at the top & left of the
.ScrollRow = ActiveCell.Row
screen screen
End With
Active Cell, Shows the value of the current
MsgBox ActiveCell.Value
Value active cell
Making your macros run
automatically when opening
Sub Auto_Open() your workbook. This macro
Auto Run (1) MsgBox "Hello" will display the message
End Sub "Hello" when you open the
workbook. This code would
be located in the module.
For this second method,
Sub Workbook_Open()
giving the same result as the
Auto Run (2) MsgBox "Hello"
previous one, the code must
End Sub
be in the workbook.
Sub Worksheet_Activate()
This one starts on activating a
Auto Run (3) MsgBox "Hello"
sheet.
End Sub
This workbook: ' = Autorun
Sub Workbook_Open()
Call SaveMe
End Sub
If the Macros are for your own use, security is usually no problem. However, if you make a
Workbook and macros to be used by others, you will find that anything that can go wrong, will
go wrong. This means that security needs to get some serious attention. The following is usually
needed at least then:
1) Hide sheets that are not needed for view by the users
2) Protect your Workbook and Sheets with passwords
3) Protect your VBA code: Point at your project in the explorer window, right click on it and
select VBA project properties, click on the Protection tab, check on Lock Project for Viewing
and next key in your password.
Please keep in mind that I am an occasional user of Excel and VBA, which is the reason I made
this overview.
In case you need to contact me about it: rob@panzerbasics.com