VBA Guide To Referencing Worksheet Tabs

Banner2.png

Referencing Worksheets in Excel

When you are working with VBA inside Excel, it is more than likely that you will be automating some sort of changes to a worksheet inside your file. The following VBA guide is intended to show you have your can target specific worksheets within your workbooks in order to apply changes to them.

 

Reference Worksheet By Code Name [BEST PRACTICE!]

Sheet1.Range("A1").Value = 100


Reference Worksheet By Name

ThisWorkbook.Worksheets("Summary Tab").Range("A1").Value = 100


Reference Currently Viewed Worksheet

ActiveSheet.Range("A1").Value = 100


Reference Worksheet By Position

ThisWorkbook.Worksheets(3).Range("A1").Value = 100


Reference Last Worksheet In Workbook

ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count).Range("A1").Value = 100


Reference Worksheet Within Another Workbook

Workbooks("Book2").Worksheets("Sheet1").Range("A1").Value = 100


Store Worksheet To A Variable

Dim sht As Worksheet

Set sht = ThisWorkbook.Worksheets("Summary Tab")


Store Newly Created Worksheet To A Variable

Dim sht As Worksheet

Set sht = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets.Count)


Loop Through Every Worksheet In A Workbook

Sub WorksheetLoop()

Dim sht As Worksheet


For Each sht In ThisWorkbook.Worksheets
  
  sht.Range("A1").Value = 100

Next sht

End Sub


Loop Through Every Worksheet In Reverse Order

Sub WorksheetReverseLoop()

Dim x As Long

'Reverse Loop Through Sheets
  For x = ThisWorkbook.Worksheets.Count To 1 Step -1
    ThisWorkbook.Worksheets(x).Range("A1").Value = 100
  Next x

End Sub


Loop Through A List Of Worksheet Names

Sub WorksheetListLoop()

Dim SheetList As Variant

'List Sheet Names into an Array Variable
  SheetList = Array("Sheet1", "Sheet4", "Sheet6")

'Loop through list
  For x = LBound(SheetList) To UBound(SheetList)
    ThisWorkbook.Worksheets(SheetList(x)).Range("A1").Value = 100
  Next x

End Sub

 

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

Chris
Founder, TheSpreadsheetGuru.com