SlideShare a Scribd company logo
1 of 1
Download to read offline
VBA Workbooks, Worksheets and Ranges
ExcelMacroMastery.com
ACCESS A WORKBOOK
The current workbook ThisWorkbook
The active workbook ActiveWorkbook
Any open workbook Workbooks("example.xlsx")
Open a workbook Workbooks.Open "C:docsbook.xlsx"
Using a variable Dim wk As Workbook
Set wk = Workbooks("example.xlsx")
USING RANGE
USING VARIABLES WITH RANGES
Row is variable Range("A" & i) Or Cells(i,1)
Column is variable Cells(1,i)
Multiple cells Range("A1:A" & i ) Or
Range(Cells(1,1),Cells(i,1))
USING CURRENT REGION
CurrentRegion Gets the entire range of data
Cells A1:D5 have
data. Get entire
range
Range("A1") .CurrentRegion Or
Range("B2") .CurrentRegion Or
Range("D5") .CurrentRegion etc.
ACCESS A WORKSHEET
In current workbook ThisWorkbook.Worksheets("name")
In current workbook Use the code name e.g. Sheet1
In a given workbook wk.Worksheets("name")
The active worksheet ActiveSheet
Worksheet variable Dim sh As Worksheet
Set sh = wk.Worksheets("name")
Access Range <worksheet>.Range
Read value from cell Total = Range("A1") .Value
Write value to cell Range("A1").Value = 5
Assign one cell to another Range("A1").Value = Range("B1")
.Value
Assign multiple cells Range("A1:C4").Value =
Range("F1:H4").Value
Read from range to array Dim arr As Variant
arr = Range("A1:B3").Value
Write from array to range Range("A1:B3").Value = arr
Assign and transpose Range("E1:H1").Value =
WorksheetFunction.Transpose
OFFSET PROPERTIES
Offset Moves range by rows and colums
Get cell to right Range("B2").Offset(0,1) ' C2
Get cell to left Range("B2").Offset(0,-1) ' A2
Get cell above Range("B2").Offset(-1,0) ' B1
Get cell below Range("B2").Offset(1,0) ' B3
Multiple cells Range("A1:B2").Offset(3,3) ' D4:E5
ROWS AND COLUMNS
Last row Cells(Rows.Count, 1).End(xlUp).row
Last column Cells(1,Columns.Count).End(xlToLeft).Column
Cell count Range("A1") .Count
Row count Range("A1:D5") .Rows.Count
Column count Range("A1:D5") .Columns.Count
Get a row <worksheet>.Rows(3)
Get a column <worksheet>.Columns(2)
READING THROUGH RANGES
Every cell in
range
Dim rg As Range
For Each rg In Range("A1:A10")
Debug.Print rg
Next rg
Read by row Dim row As Range
For Each row In Range("A1:A5").Rows
' Print cell in third column of row
Debug.Print row.Cells(1,3)
Next row
READING THROUGH OTHER ITEMS
All Open workbooks Dim wk As Workbook
For Each wk In Workbooks
Debug.Print wk.Name
Next wk
All worksheets in a
workbook
Set wk = Workbooks("Test.xlsx”)
Dim sh As Worksheet
For Each sh In wk.Worksheets
Debug.Print sh.Name
Next sh
USEFUL PROPERTIES FOR TESTING
Worksheet of a range Range("A1") .Parent
Workbook of a worksheet Worksheets(1).Parent
Workbook of a range Range("A1").Parent.Parent
Workbook name Workbooks(1).Name
Workbook path Workbooks(1).Path
Workbook path + name Workbooks(1).FullName
Current user Application.UserName
COPYING RANGES
Everything Range("A1:A5").Copy Range("C1:C5")
Paste Special Range("A1:A5").Copy
Range("C1:C5").PasteSpecial xlPasteFormats
Range("C1:C5").PasteSpecial xlPasteValues

More Related Content

What's hot

Obiee 11g installation
Obiee 11g installationObiee 11g installation
Obiee 11g installation
Amit Sharma
 

What's hot (20)

Oracle REST Data Services Best Practices/ Overview
Oracle REST Data Services Best Practices/ OverviewOracle REST Data Services Best Practices/ Overview
Oracle REST Data Services Best Practices/ Overview
 
Azure DataBricks for Data Engineering by Eugene Polonichko
Azure DataBricks for Data Engineering by Eugene PolonichkoAzure DataBricks for Data Engineering by Eugene Polonichko
Azure DataBricks for Data Engineering by Eugene Polonichko
 
Architect day 20181128- Morning Sessions
Architect day 20181128- Morning SessionsArchitect day 20181128- Morning Sessions
Architect day 20181128- Morning Sessions
 
Making External Web Pages Interact With Visualforce
Making External Web Pages Interact With VisualforceMaking External Web Pages Interact With Visualforce
Making External Web Pages Interact With Visualforce
 
ETL Testing Training Presentation
ETL Testing Training PresentationETL Testing Training Presentation
ETL Testing Training Presentation
 
SmartERP Oracle Cloud Capabilities presentation 2018
SmartERP Oracle Cloud Capabilities presentation 2018SmartERP Oracle Cloud Capabilities presentation 2018
SmartERP Oracle Cloud Capabilities presentation 2018
 
Oracle Data Integrator 12c
Oracle Data Integrator 12cOracle Data Integrator 12c
Oracle Data Integrator 12c
 
Microsoft Azure Data Factory Data Flow Scenarios
Microsoft Azure Data Factory Data Flow ScenariosMicrosoft Azure Data Factory Data Flow Scenarios
Microsoft Azure Data Factory Data Flow Scenarios
 
Appian Self assembly Language
Appian  Self assembly LanguageAppian  Self assembly Language
Appian Self assembly Language
 
Azure Data Factory Introduction.pdf
Azure Data Factory Introduction.pdfAzure Data Factory Introduction.pdf
Azure Data Factory Introduction.pdf
 
SSIS control flow
SSIS control flowSSIS control flow
SSIS control flow
 
Building Cloud Native Applications with Oracle Autonomous Database.
Building Cloud Native Applications with Oracle Autonomous Database.Building Cloud Native Applications with Oracle Autonomous Database.
Building Cloud Native Applications with Oracle Autonomous Database.
 
Obiee 11g installation
Obiee 11g installationObiee 11g installation
Obiee 11g installation
 
Oracle Data integrator 11g (ODI) - Online Training Course
Oracle Data integrator 11g (ODI) - Online Training Course Oracle Data integrator 11g (ODI) - Online Training Course
Oracle Data integrator 11g (ODI) - Online Training Course
 
Power bi premium
Power bi premiumPower bi premium
Power bi premium
 
Adf presentation
Adf presentationAdf presentation
Adf presentation
 
OData service from ABAP CDS
OData service from ABAP CDSOData service from ABAP CDS
OData service from ABAP CDS
 
Informatica session
Informatica sessionInformatica session
Informatica session
 
Melbourne UG Presentation - UI Flow for Power Automate
Melbourne UG Presentation - UI Flow for Power AutomateMelbourne UG Presentation - UI Flow for Power Automate
Melbourne UG Presentation - UI Flow for Power Automate
 
Azure data factory
Azure data factoryAzure data factory
Azure data factory
 

Similar to Vba vitals cheat sheet

1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
christiandean12115
 
I have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docxI have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docx
delciegreeks
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
Frankie Jones
 

Similar to Vba vitals cheat sheet (20)

cheatsheet from DataCamp.pdf
cheatsheet from DataCamp.pdfcheatsheet from DataCamp.pdf
cheatsheet from DataCamp.pdf
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
 
Chapter 4: Vector Spaces - Part 4/Slides By Pearson
Chapter 4: Vector Spaces - Part 4/Slides By PearsonChapter 4: Vector Spaces - Part 4/Slides By Pearson
Chapter 4: Vector Spaces - Part 4/Slides By Pearson
 
Excel tutorial for frequency distribution
Excel tutorial for frequency distributionExcel tutorial for frequency distribution
Excel tutorial for frequency distribution
 
Excel tutorial for frequency distribution
Excel tutorial for frequency distributionExcel tutorial for frequency distribution
Excel tutorial for frequency distribution
 
I have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docxI have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docx
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
Excel.useful fns
Excel.useful fnsExcel.useful fns
Excel.useful fns
 
R-Data table Cheat Sheet
R-Data table Cheat SheetR-Data table Cheat Sheet
R-Data table Cheat Sheet
 
SQL report
SQL reportSQL report
SQL report
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
R gráfico
R gráficoR gráfico
R gráfico
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Advanced Excel Courses Mumbai
Advanced Excel Courses MumbaiAdvanced Excel Courses Mumbai
Advanced Excel Courses Mumbai
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Advanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment TechnologiesAdvanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment Technologies
 
04.formula and fuction
04.formula and fuction04.formula and fuction
04.formula and fuction
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

Vba vitals cheat sheet

  • 1. VBA Workbooks, Worksheets and Ranges ExcelMacroMastery.com ACCESS A WORKBOOK The current workbook ThisWorkbook The active workbook ActiveWorkbook Any open workbook Workbooks("example.xlsx") Open a workbook Workbooks.Open "C:docsbook.xlsx" Using a variable Dim wk As Workbook Set wk = Workbooks("example.xlsx") USING RANGE USING VARIABLES WITH RANGES Row is variable Range("A" & i) Or Cells(i,1) Column is variable Cells(1,i) Multiple cells Range("A1:A" & i ) Or Range(Cells(1,1),Cells(i,1)) USING CURRENT REGION CurrentRegion Gets the entire range of data Cells A1:D5 have data. Get entire range Range("A1") .CurrentRegion Or Range("B2") .CurrentRegion Or Range("D5") .CurrentRegion etc. ACCESS A WORKSHEET In current workbook ThisWorkbook.Worksheets("name") In current workbook Use the code name e.g. Sheet1 In a given workbook wk.Worksheets("name") The active worksheet ActiveSheet Worksheet variable Dim sh As Worksheet Set sh = wk.Worksheets("name") Access Range <worksheet>.Range Read value from cell Total = Range("A1") .Value Write value to cell Range("A1").Value = 5 Assign one cell to another Range("A1").Value = Range("B1") .Value Assign multiple cells Range("A1:C4").Value = Range("F1:H4").Value Read from range to array Dim arr As Variant arr = Range("A1:B3").Value Write from array to range Range("A1:B3").Value = arr Assign and transpose Range("E1:H1").Value = WorksheetFunction.Transpose OFFSET PROPERTIES Offset Moves range by rows and colums Get cell to right Range("B2").Offset(0,1) ' C2 Get cell to left Range("B2").Offset(0,-1) ' A2 Get cell above Range("B2").Offset(-1,0) ' B1 Get cell below Range("B2").Offset(1,0) ' B3 Multiple cells Range("A1:B2").Offset(3,3) ' D4:E5 ROWS AND COLUMNS Last row Cells(Rows.Count, 1).End(xlUp).row Last column Cells(1,Columns.Count).End(xlToLeft).Column Cell count Range("A1") .Count Row count Range("A1:D5") .Rows.Count Column count Range("A1:D5") .Columns.Count Get a row <worksheet>.Rows(3) Get a column <worksheet>.Columns(2) READING THROUGH RANGES Every cell in range Dim rg As Range For Each rg In Range("A1:A10") Debug.Print rg Next rg Read by row Dim row As Range For Each row In Range("A1:A5").Rows ' Print cell in third column of row Debug.Print row.Cells(1,3) Next row READING THROUGH OTHER ITEMS All Open workbooks Dim wk As Workbook For Each wk In Workbooks Debug.Print wk.Name Next wk All worksheets in a workbook Set wk = Workbooks("Test.xlsx”) Dim sh As Worksheet For Each sh In wk.Worksheets Debug.Print sh.Name Next sh USEFUL PROPERTIES FOR TESTING Worksheet of a range Range("A1") .Parent Workbook of a worksheet Worksheets(1).Parent Workbook of a range Range("A1").Parent.Parent Workbook name Workbooks(1).Name Workbook path Workbooks(1).Path Workbook path + name Workbooks(1).FullName Current user Application.UserName COPYING RANGES Everything Range("A1:A5").Copy Range("C1:C5") Paste Special Range("A1:A5").Copy Range("C1:C5").PasteSpecial xlPasteFormats Range("C1:C5").PasteSpecial xlPasteValues