Writing VBA Macros Record a new Macro, recording only the name, shortcut and description Stop recording Open macro for editing and enter VB code Test
Moving around the Spreadsheet To move the cursor to a particular cell (starting point) Range(“A4”).select Range(“NamedCell”).select This is now the “Active Cell”
Referencing a Cell from ActiveCell ActiveCell.offset (R,C) e.g. ActiveCell.offset(0,-1) refers to the cell one to the left of the ActiveCell
To place information from a VB variable into a spreadsheet Range(“D4”) = TaxRate Range(“Result”) = Sum ActiveCell = Salary * Rate ActiveCell.offset(12,0) = totalCost ActiveSheet.Cells(11,9) = Total Worksheets(“MySheet”).Cells(1,1) = title Sheet2.cells(1,3) = Title 'Refers to sheet 2 row1,column 3
To input from a spreadsheet to variable Amount = ActiveCell Amount = ActiveCell.Offset(0,-3) Amount =ActiveSheet.Cells(9,1) 'current sheet Row 9, column 1
Loops Do until ActiveCell.offset(Row, 0) = “” 'put your actions here Row = Row +1 Loop
Other Features Assigning Macros to Buttons Rather than using Ctrl + Character all the time More handy to have a button Insert Shapes Draw button Right Click on the button Assign macro Any of the VB commands can be used InputBox ActiveCell = InputBox(“Enter you name”,”Namebox”) Msgbox