0% found this document useful (0 votes)
186 views

VBA Vitals Cheat Sheet

This document discusses working with workbooks, worksheets, ranges, rows, columns, and cells in VBA. It covers opening and accessing workbooks and worksheets, selecting ranges, reading and writing cell values, iterating through ranges and collections, and useful properties like parent, name, and path. Offset is described for moving ranges, and copying/pasting ranges and values is demonstrated.

Uploaded by

Nilu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

VBA Vitals Cheat Sheet

This document discusses working with workbooks, worksheets, ranges, rows, columns, and cells in VBA. It covers opening and accessing workbooks and worksheets, selecting ranges, reading and writing cell values, iterating through ranges and collections, and useful properties like parent, name, and path. Offset is described for moving ranges, and copying/pasting ranges and values is demonstrated.

Uploaded by

Nilu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

VBA Workbooks, Worksheets and Ranges

ACCESS A WORKBOOK USING CURRENT REGION


The current workbook ThisWorkbook CurrentRegion Gets the entire range of data
The active workbook ActiveWorkbook Cells A1:D5 have Range("A1") .CurrentRegion Or
data. Get entire Range("B2") .CurrentRegion Or
Any open workbook Workbooks("example.xlsx") range Range("D5") .CurrentRegion etc.
Open a workbook Workbooks.Open "C:\docs\book.xlsx"
Using a variable Dim wk As Workbook ROWS AND COLUMNS
Set wk = Workbooks("example.xlsx") Last row Cells(Rows.Count, 1).End(xlUp).row

Last column Cells(1,Columns.Count).End(xlToLeft).Column


ACCESS A WORKSHEET
Cell count Range("A1") .Count
In current workbook ThisWorkbook.Worksheets("name")
Row count Range("A1:D5") .Rows.Count
In current workbook Use the code name e.g. Sheet1
In a given workbook wk.Worksheets("name") Column count Range("A1:D5") .Columns.Count

The active worksheet ActiveSheet Get a row <worksheet>.Rows(3)


Get a column <worksheet>.Columns(2)
Worksheet variable Dim sh As Worksheet
Set sh = wk.Worksheets("name")
READING THROUGH RANGES
USING RANGE Every cell in Dim rg As Range
Access Range <worksheet>.Range range For Each rg In Range("A1:A10")
Debug.Print rg
Read value from cell Total = Range("A1")
Next rg
Write value to cell Range("A1") = 5
Read by row Dim row As Range
Assign one cell to another Range("A1") = Range("B1") For Each row In Range("A1:A5").Rows
Assign multiple cells Range("A1:C4") = ' Print cell in third column of row
Range("F1:H4").Value Debug.Print row.Cells(1,3)
Next row
Read from range to array Dim arr As Variant
arr = Range("A1:B3") READING THROUGH OTHER ITEMS

Write from array to range Range("A1:B3") = arr All Open workbooks Dim wk As Workbook
For Each wk In Workbooks
Assign and transpose Range("E1:H1") = Debug.Print wk.Name
WorksheetFunction.Transpose Next wk
(Range("A1:B4"))
All worksheets in a Set wk = Workbooks("Test.xlsx”)
workbook Dim sh As Worksheet
USING VARIABLES WITH RANGES
For Each sh In wk.Worksheets
Row is variable Range("A" & i) Or Cells(i,1) Debug.Print sh.Name
Next sh
Column is variable Cells(1,i)
USEFUL PROPERTIES FOR TESTING
Multiple cells Range("A1:A" & i ) Or
Range(Cells(1,1),Cells(i,1)) Worksheet of a range Range("A1") .Parent
Workbook of a worksheet Worksheets(1).Parent
OFFSET PROPERTIES
Workbook of a range Range("A1").Parent.Parent
Offset Moves range by rows and colums
Workbook name Workbooks(1).Name
Get cell to right Range("B2").Offset(0,1) ' C2
Workbook path Workbooks(1).Path
Get cell to left Range("B2").Offset(0,-1) ' A2
Workbook path + name Workbooks(1).FullName
Get cell above Range("B2").Offset(-1,0) ' B1 Current user Application.UserName
COPYING RANGES
Get cell below Range("B2").Offset(1,0) ' B3
Everything Range("A1:A5").Copy Range("C1:C5")
Multiple cells Range("A1:B2").Offset(3,3) ' D4:E5
Paste Special Range("A1:A5").Copy
Range("C1:C5").PasteSpecial xlPasteFormats
Range("C1:C5").PasteSpecial xlPasteValues
ExcelMacroMastery.com

You might also like