Get the Developer Tab and Intro to Developer Tab
open Excel and create a new blank worksheet
to get developer tab
- to open options dialog follow path File|Options
- click Customize Ribbon
- in right pane check the Developer check box
- close the dialog
Top
Index
Getting Started with Range Object
range can be a single cell or a series of cell e.g. B2:C6 which would contain cells
B2, B3, B4, B5, B6, C2, C3, C4, C5, C6
Top
Index
Quick Tip - Project Explorer and Properties Window
keyboard short cuts for VBA Editor
- Alt-F11 - opens VBA Editor
- Ctrl-R - opens Project Explorer pane
- F4 - opens Properties pane
Top
Index
Creating Your First Macro From Scratch
open VBA Editor
from Insert menu choice click Module
in the editor create a macro
Sub blah()
range("A1:C6") = 12
End Sub
use F5 to run the macro
cells in the range will be populated by '12'
Top
Index
Adding a Secondary Command
modify the macro as shown below
Sub blah()
range("A1:C6") = 12
range("B5") = 130
End Sub
when run the macro first sets the cells in the range to 12
the second command sets the cell B5 to 130
Top
Index
use F8 to step through a macro line by line
Top
Index
Saving a Macro-enabled Workbook (.xlsm)
typical workbooks use the extension xlsx
macro-enabled workbooks use the extension xlsm
when saving a macro-enabled workbook change the 'Save as type' dropdown to Excel
Macro-Enabled Workbook in the save dialog
Top
Index
Before You Begin the Exercises
how to add a button
- on Developer tab click Insert and click the ActiveX button
- draw the button on the worksheet
- right click the button and select the Properties from the context menu
- change caption and name as desired
- double click the button to add a method to the worksheet
Private Sub ClickMeButton_Click()
// insert code here
End Sub
insert a rounded-corner rectangle shape into worksheet
type text directly into the shape
right-click on the shape and click Assign Macro from the context menu
in the resulting dialog select the macro to be run when the shape is clicked
how to record a macro
- on Developer tab click 'Record Macro'
- do whateve to the worksheet
- when done click 'Stop Recording'
in the VBA Editor a new module will be added
the module will contain all the actions recorded
Top
Index
Several ways to trigger your Macro
the Macro glyph on the Developers tab or Alt-F8 will open a modal dialog which lists
the macros
a macro can be run by double clicking the name or selecting the macro and clicking
the Run button
Top
Index
Affecting Multiple Cells with One Range Command - 2 Methods
the macro below shows two ways to change multiple cells with one range command
Sub multiCellsSingleRange()
Range("A1,B2") = 12
Range("C1:D5, A3") = 23
End Sub
Top
Index
Adding Strings of Text to a Cell
to put text in a cell wrap the string with double quotes
Sub myText()
Range("A1") = "some text"
End Sub
Top
Index
Using a Named Range with the Range Object
select a range of cells in the worksheet
name the range in the Name textbox (upper left)
to use the named range in a macro
Sub myNamedRange()
Range("<range name>") = 123
End Sub
Top
Index
Sub Exercise1()
Range("A1") = "ID"
Range("B1") = "First Name"
Range("C1") = "Last Name"
Range("A1:C1").Font.Bold = True
End Sub
Top
Index