How do I Print multiple sheets in Excel macro?

If you are new to VBA then this post is a great place to start. I like to break things down into simple terms and explain them in plain English without the jargon.

You can read through the post from start to finish as it is written in a logical order. If you prefer, you can use the table of contents below and go directly to the topic of your choice.

 
 

Contents

  • 1 A Quick Guide to the VBA Worksheet
  • 2 Introduction
  • 3 Accessing the Worksheet
    • 3.1 Hide Worksheet
    • 3.2 Protect Worksheet
    • 3.3 Subscript Out of Range
  • 4 Using the Index  to Access the Worksheet
  • 5 Using the Code Name of a Worksheet
    • 5.1 Code Name in other Workbooks
    • 5.2 Code Name Summary
  • 6 The Active Sheet
  • 7 Declaring a Worksheet Object
  • 8 Accessing the Worksheet in a Nutshell
  • 9 Add Worksheet
  • 10 Delete Worksheet
  • 11 Loop Through the Worksheets
  • 12 Using the Sheets Collection
  • 13 Conclusion
  • 14 What’s Next?

A Quick Guide to the VBA Worksheet

The following table gives a quick run down to the different worksheet methods.

Note: I use Worksheets in the table below without specifying the workbook i.e.Worksheets rather than ThisWorkbook.Worksheets, wk.Worksheets etc. This is to make the examples clear and easy to read. You should always specify the workbook when using Worksheets. Otherwise the active workbook will be used by default.

 

TaskHow toAccess worksheet by nameWorksheets("Sheet1")Access worksheet by position from leftWorksheets(2)
Worksheets(4)Access the left most worksheetWorksheets(1)Access the right most worksheetWorksheets(Worksheets.Count)Access using worksheet code name(current workbook only)see Code Name section belowAccess using worksheet code name(other workbook)see Code Name section belowAccess the active worksheetActiveSheetDeclare worksheet variableDim sh As WorksheetAssign worksheet variableSet sh = Worksheets("Sheet1")Add worksheetWorksheets.AddAdd worksheet and assign to variableSet sh =Worksheets.AddAdd worksheet to first position(left)Worksheets.Add Before:=Worksheets(1)Add worksheet to last position(right)Worksheets.Add after:=Worksheets(Worksheets.Count)Add multiple worksheetsWorksheets.Add Count:=3Activate Worksheetsh.ActivateCopy Worksheetsh.CopyCopy after a worksheetsh1.Copy After:=Sh2Copy before a worksheetsh1.Copy Before:=Sh2Delete Worksheetsh.DeleteDelete Worksheet without warningApplication.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
Change worksheet namesh.Name = "Data"Show/hide worksheetsh.Visible = xlSheetHidden
sh.Visible = xlSheetVisibleLoop through all worksheets(For)Dim i As Long
For i = 1 To Worksheets.Count
    Debug.Print Worksheets(i).Name
Next iLoop through all worksheets(For Each)Dim sh As Worksheet
For Each sh In Worksheets
    Debug.Print sh.Name
Next

 
 

Introduction

The three most important elements of VBA are the Workbook, the Worksheet and Cells. Of all the code your write, 90% will involve one or all of them.

The most common use of the worksheet in VBA is for accessing its cells. You may use it to protect, hide, add, move or copy a worksheet. However, you will mainly use it to perform some action on one or more cells on the worksheet.

 
 
Using Worksheets is more straightforward than using workbooks. With workbooks you may need to open them, find which folder they are in, check if they are in use and so on. With a worksheet, it either exists in the workbook or it doesn’t.

 
 

Accessing the Worksheet

In VBA, each workbook has a collection of worksheets. There is an entry in this collection for each worksheet in the workbook. This collection is simply called Worksheets and is used in a very similar way to the Workbooks collection. To get access to a worksheet all you have to do is supply the name.

 
 
The code below writes “Hello World” in Cell A1 of Sheet1, Sheet2 and Sheet3 of the current workbook.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Write To cell A1 In Sheet1,Sheet2 And Sheet3
    ThisWorkbook.Worksheets("Sheet1").Range("A1") = "Hello World"
    ThisWorkbook.Worksheets("Sheet2").Range("A1") = "Hello World"
    ThisWorkbook.Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub

 
 
The Worksheets collection is always belong to a workbook. If we don’t specify the workbook then the active workbook is used by default.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub

 
 

Hide Worksheet

The following examples show how to hide and unhide a worksheet

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible

 
 
If you want to prevent a user accessing the worksheet, you can make it “very hidden”. This means it can only be made visible by the code.

' Hide from user access
ThisWorkbook.Worksheets("Sheet1").Visible = xlVeryHidden

' This is the only way to make a xlVeryHidden sheet visible
ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible

 
 

Protect Worksheet

Another example of using the worksheet is when you want to protect it

ThisWorkbook.Worksheets("Sheet1").Protect Password:="MyPass"

ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="MyPass"

 
 

Subscript Out of Range

When you use Worksheets you may get the error:

Run-time Error 9 Subscript out of Range

 
 

How do I Print multiple sheets in Excel macro?

 
 
This means you tried to access a worksheet that doesn’t exist. This may happen for the following reasons

  1. The worksheet name given to Worksheets is spelled incorrectly.
  2. The name of the worksheet has changed.
  3. The worksheet was deleted.
  4. The index was to large e.g. You used Worksheets(5) but there are only four worksheets
  5. The wrong workbook is being used e.g. Workbooks(“book1.xlsx”).Worksheets(“Sheet1”) instead of Workbooks(“book3.xlsx”).Worksheets(“Sheet1”).

 
 
If you still have issues then use one of the loops from Loop Through The Worksheets section to print the names of all worksheets the collection.

 
 

Using the Index  to Access the Worksheet

So far we have been using the sheet name to access the sheet. The index refers to the sheet tab position in the workbook. As the position can easily be changed by the user it is not a good idea to use this.

 
 
The following code shows examples of using the index

' https://excelmacromastery.com/
' Using this code is a bad idea as
' sheet positions changes all the time
Public Sub UseSheetIdx()

    With ThisWorkbook
        ' Left most sheet
        Debug.Print .Worksheets(1).Name
        ' The third sheet from the left
        Debug.Print .Worksheets(3).Name
        ' Right most sheet
        Debug.Print .Worksheets(.Worksheets.Count).Name
    End With

End Sub

 
 
In the example above, I used Debug.Print to print to the Immediate Window. To view this window select View->Immediate Window(or Ctrl G)

How do I Print multiple sheets in Excel macro?

 
 

How do I Print multiple sheets in Excel macro?

 
 

Using the Code Name of a Worksheet

The best method of accessing the worksheet is using the code name. Each worksheet has a sheet name and a code name. The sheet name is the name that appears in the worksheet tab in Excel.

Changing the sheet name does not change the code name meaning that referencing a sheet by the code name is a good idea.

 
 
If you look in the VBE property window you will see both names. In the image you can see that the code name is the name outside the parenthesis and the sheet name is in the parenthesis.

How do I Print multiple sheets in Excel macro?

 
 
You can change both the sheet name and the code name in the property window of the sheet(see image below).

How do I Print multiple sheets in Excel macro?

 
 
If your code refers to the code name then the user can change the name of the sheet and it will not affect your code. In the example below we reference the worksheet directly using the code name.

' https://excelmacromastery.com/
Public Sub UseCodeName2()

    ' Using the code name of the worksheet
    Debug.Print CodeName.Name
    CodeName.Range("A1") = 45
    CodeName.Visible = True

End Sub

 
 
This makes the code easy to read and safe from the user changing the sheet name.

 
 

Code Name in other Workbooks

There is one drawback to using the code name. It can only refer to worksheets in the workbook that contains the code i.e. ThisWorkbook.

 
 
However, we can use a simple function to find the code name of a worksheet in a different workbook.

' https://excelmacromastery.com/
Public Sub UseSheet()

    Dim sh As Worksheet
    ' Get the worksheet using the codename
    Set sh = SheetFromCodeName("CodeName", ThisWorkbook)
    ' Use the worksheet
    Debug.Print sh.Name

End Sub

' This function gets the worksheet object from the Code Name
Public Function SheetFromCodeName(Name As String, bk As Workbook) As Worksheet

    Dim sh As Worksheet
    For Each sh In bk.Worksheets
        If sh.CodeName = Name Then
           Set SheetFromCodeName = sh
           Exit For
        End If
    Next sh

End Function

 
 
Using the above code means that if the user changes the name of the worksheet then your code will not be affected.

There is another way of getting the sheet name of an external workbook using the code name. You can use the VBProject element of that Workbook.

 
 
You can see how to do this in the example below. I have included this for completeness only and I would recommend using the method in the previous example rather than this one.

' https://excelmacromastery.com/
Public Function SheetFromCodeName2(codeName As String _
                             , bk As Workbook) As Worksheet

    ' Get the sheet name from the CodeName using the VBProject
    Dim sheetName As String
    sheetName = bk.VBProject.VBComponents(codeName).Properties("Name")

    ' Use the sheet name to get the worksheet object
    Set SheetFromCodeName2 = bk.Worksheets(sheetName)

End Function

 
 

Code Name Summary

The following is a quick summary of using the Code Name

  1. The code name of the worksheet can be used directly in the code e.g. Sheet1.Range
  2. The code name will still work if the worksheet name is changed.
  3. The code name can only be used for worksheets in the same workbook as the code.
  4. Anywhere you see ThisWorkbook.Worksheets(“sheetname”) you can replace it with the code name of the worksheet.
  5. You can use the SheetFromCodeName function from above to get the code name of worksheets in other workbooks.

 
 

The Active Sheet

The ActiveSheet object refers to the worksheet that is currently active. You should only use ActiveSheet if you have a specific need to refer to the worksheet that is active.

Otherwise you should specify the worksheet you are using.

If you use a worksheet method like Range and don’t mention the worksheet, it will use the active worksheet by default.

' Write to Cell A1 in the active sheet
ActiveSheet.Range("A1") = 99

' Active sheet is the default if no sheet used
Range("A1") = 99

 
 

Declaring a Worksheet Object

Declaring a worksheet object is useful for making your code neater and easier to read.

The next example shows code for updating ranges of cells. The first Sub does not declare a worksheet object. The second sub declares a worksheet object and the code is therefore much clearer.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
0

 
 

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
1

 
 
You could also use the With keyword with the worksheet object as the next example shows.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
2

 
 

Accessing the Worksheet in a Nutshell

With all the different ways to access a worksheet, you may be feeling overwhelmed or confused. So in this section, I am going to break it down into simple terms

 
 
1. If you want to use whichever worksheet is currently active then use ActiveSheet.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
3

 
 
2. If the worksheet is in the same workbook as the code then use the Code Name.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
4

 
 
3. If the worksheet is in a different workbook then first get workbook and then get the worksheet.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
5

 
 
If you want to protect against the user changing the sheet name then use the SheetFromCodeName function from the Code Name section.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
6

 
 

Add Worksheet

The examples in this section show you how to add a new worksheet to a workbook. If you do not supply any arguments to the Add function then the new worksheet will be placed before the active worksheet.

When you add a Worksheet, it is created with a default name like “Sheet4”. If you want to change the name then you can easily do this using the Name property.

 
 
The following example adds a new worksheet and changes the name to “Accounts”. If a worksheet with the name “Accounts” already exists then you will get an error.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
7

 
 
In the previous example, you are adding worksheets in relation to the active worksheet. You can also specify the exact position to place the worksheet.

 
 
To do this you need to specify which worksheet the new one should be inserted before or after. The following code shows you how to do this.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
8

 
 

Delete Worksheet

To delete a worksheet you simply call the Delete member.

' https://excelmacromastery.com/
Public Sub WriteToCell1()

    ' Worksheets refers to the worksheets in the active workbook
    Worksheets("Sheet1").Range("A1") = "Hello World"
    Worksheets("Sheet2").Range("A1") = "Hello World"
    Worksheets("Sheet3").Range("A1") = "Hello World"

End Sub
9

 
 
Excel will display a warning message when you delete a worksheet. If you want to hide this message you can use the code below

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
0

 
 
There are two issues to watch out for when it comes to deleting worksheets.

If you try to access the worksheet after deleting it you will get the “Subscript out of Range” error we saw in the Accessing the Worksheet section.

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
1

 
 
The second issue is when you assign a worksheet variable. If you try to use this variable after the worksheet is deleted then you will get an Automation error like this

Run-Time error -21147221080 (800401a8′) Automation Error

If you are using the Code Name of the worksheet rather than a variable, then this will cause Excel to crash rather than give the automation error.

 
 
The following example shows how an automation errors occurs

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
2

 
 

If you assign the Worksheet variable to a valid worksheet it will work fine

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
3

 
 

Loop Through the Worksheets

The Worksheets member of Workbooks is a collection of worksheets belonging to a workbook. You can go through each sheet in the worksheets collection using a For Each Loop or a For Loop.

 
 
The following example uses a For Each loop.

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
4

 
 
The next example uses the standard For loop

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
5

 
 
You have seen how to access all open workbooks and how to access all worksheets in ThisWorkbook. Lets take it one step further. Lets access all worksheets in all open workbooks.

Note: If you use code like this to write to worksheets then back everything up first as you could end up writing the incorrect data to all the sheets.

 
 

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
6

 
 

Using the Sheets Collection

The workbook has another collection similar to Worksheets called Sheets. This causes confusion at times among users. To explain this first you need to know about a sheet type that is a chart.

 
 
It is possible in Excel to have a sheet that is a chart. To do this

  1. Create a chart on any sheet.
  2. Right click on the chart and select Move.
  3. Select the first option which is “New Sheet” and click Ok.

 
 
Now you have a workbook with sheets of type worksheet and one of type chart.

  • The Worksheets collection refers to all worksheets in a workbook. It does not include sheets of type chart.
  • The Sheets collection refers to all sheets belonging to a workbook including sheets of type chart.

 
 
There are two code examples below. The first goes through all the Sheets in a workbook and prints the name of the sheet and type of sheet it is. The second example does the same with the Worksheets collection.

 
 
To try out these examples you should add a Chart sheet to your workbook first so you will see the difference.

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetHidden

ThisWorkbook.Worksheets("Sheet1").Visible = xlSheetVisible
7

 
 
If do not have chart sheets then using the Sheets collection is the same as using the Worksheets collection.

 
 

Conclusion

This concludes the post on the VBA Worksheet. I hope you found it useful.

The three most important elements of Excel VBA are Workbooks, Worksheets and Ranges and Cells. These elements will be used in almost everything you do.  Understanding them will make you life much easier and make learning VBA much simpler.

 
 

What’s Next?

Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA skills then why not try out the The Ultimate VBA Tutorial.

Related Training: Get full access to the Excel VBA training webinars and all the tutorials.

(NOTE: Planning to build or manage a VBA Application? Learn how to build 10 Excel VBA applications from scratch.)