Professional Documents
Culture Documents
Wa0033
Wa0033
Wa0033
• All open Workbooks – within each workbook (which is also called a project), you can
have the following objects:
• Worksheet object for each worksheet in the workbook
• ThisWorkbook object which represents the workbook itself
• Chartsheet object for each chart sheet (these are not as common as worksheets)
• Modules – This is where the code that is generated with a macro recorder goes.
You can also write or copy-paste VBA code here.
Properties Window
MsgBox Worksheets.Count
Workbooks.Add
Workbooks("close-open.xlsm").Close
Workbook Objects
MsgBox Worksheets.Count
Workbooks.Add
Workbooks("close-open.xlsm").Close
Workbooks.Open ("sales.xlsx")
Range Objects
The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important object of Excel
VBA.
Range("B3").Value = 2
Range("A1:A4").Value = 5
Range("A1:A2,B3:C4").Value = 10
Range("Prices").Value = 15
Cells(3, 2).Value = 2
Range(Cells(1, 1), Cells(4, 1)).Value = 5
Dim example As Range
Dim example As Range Set example = Range("A1:C4")
Set example = Range("A1:C4")
example.Rows(3).Select
example.Value = 8
Dim example As Range
Worksheets(3).Activate Set example = Range("A1:C4")
Worksheets(3).Range("B7").Select
example.Columns(2).Select
Range("A1:A2").Select
Selection.Copy
Range("C3").Select
ActiveSheet.Paste
Range("C3:C4").Value = Range("A1:A2").Value
Range("A1").ClearContents
Range("A1").Value = ""
MsgBox example.Count
Variables
Dim x As Integer
x=6
Range("A1").Value = x
Range("B1").Value = result
Range("B1").Value = result
Input Box
Dim example As Variant
example = InputBox("Give me some input")
Range("A1").Value = example
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100 +i
Next i
End Sub
Offset
Using OFFSET with the range object, you can navigate from one cell to another in the worksheet and you
can also select a cell or a range. It also gives you access to the properties and methods that you have with
the range object to use, but you need to specify the arguments in the OFFSET to use it.
For i = 1 To 5 Step 2
For j = 1 To 5 Step 2
Cells(i, j).Interior.ColorIndex = 15
Next j
Next i
Create Pattern
Cells(i, j).Offset(1, 1).Interior.ColorIndex = 15
Else
temp = rng.Cells(i)
rng.Cells(i) = rng.Cells(j)
rng.Cells(j) = temp
End If
Next j
Next i
Resize
Resize is a property available in VBA to change or resize the range of cells from the active cell as needed.
For example, assume you are in cell B5. If you want to select 3 rows and two columns from this cell, we can
change the range size by using the RESIZE property of VBA.
If one of the arguments from row size or column size is not provided with this function selects the entire row or the
column from the range.
Range.Resize(number_of_rows, number_of_columns)
Range(“B2:C4").Resize(3, 2).Select
Range("A1:C4").Resize(, 1).Select
Range("A1:C4").Resize(3, 2).Select
Worksheets("Sheet1").Activate
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.Resize(numRows + 2, numColumns + 2).Select
Range("A2:B3").Select
Range("A2:B3").Resize(1, 2).Offset(-1, 1).Select
Range("A2:B3").Offset(-1, 1).Resize(1, 2).Select
The Resize property will always use the lower-line and lower-column cell as the starting point. The size
of the Range reference is irrelevant:
Range("A1:J20").Resize(2, 2).Select
Union and Intersect
• The Union method in Excel VBA returns a Range object that
represents the union of two or more ranges
Union(Range("B2:C7"), Range("C6:F8")).Select
• Range("B3").CurrentRegion.Select
Entire Rows and Columns
• Cells.Select
Worksheets(2).Activate
Worksheets(2).Cells.Select
Columns(2).Select
Rows(7).Select
Rows("5:7").Select
Columns("B:E").Select
ActiveCell.EntireRow.Select
ActiveCell.EntireColumn.Cells(1).Value = 2
ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3