Loop through All Worksheets in Excel using VBA and Macros

Add to Favorites

Ill show you how to loop through all of the worksheets in a workbook in Excel using VBA and Macros.

This only takes a few lines of code and is rather simple to use once you understand it.

Here is the macro that will loop through all worksheets in a workbook in Excel:

Sub Sheet_Loop()
'count the number of worksheets in the workbook
sheet_count = ActiveWorkbook.Worksheets.Count

'loop through the worksheets in the workbook
For a = 1 To sheet_count

    'code that you want to run on each sheet
    'simple message box that outputs the name of the sheet
    MsgBox ActiveWorkbook.Worksheets(a).Name

Next a
End Sub

Now, Ill go through the macro step-by-step.


This line is what counts the number of worksheets that are in the workbook.

The first part of the line simply sets the variable sheet_count equal to the number of sheets in the workbook:

sheet_count = ActiveWorkbook.Worksheets.Count

Now that we know how many worksheets there are, we can loop through them.

We are going to use a very simple For Next loop in this case and you can copy it directly from here into your project.

For a = 1 To sheet_count
'code that you want to run on each sheet
Next a

In the above lines we are creating a new variable a and setting it equal to 1.  We then use the sheet_count variable to tell the macro when to stop looping; remember that it holds the numeric value of how many sheets there are in the workbook.

After this first line, which creates the loop, we then put all of the code that we want to run on each worksheet.

Dont forget that, at the end of the loop we still need something:

Next a

This tells the For loop that it should increment the value of the a variable by 1 each time the loop runs through a cycle or goes through a sheet.  The loop will not work without this line of code at the end of it.

In the original example we also have a line of code within the For loop:

MsgBox ActiveWorkbook.Worksheets(a).Name

This line will output the name of each worksheet into a pop-up message box; it also illustrates how you can access the worksheets from within the loop.

To do anything with the sheets from within the loop, we need to access each sheet by its reference or index number.  Each sheet has an index number and it always starts at 1 and increments by 1 for the next sheet in the workbook.

This is why we create the a variable and set it equal to 1 in the For loop, because the first sheet in the workbook always has an index number of 1, and we want to use a as the index number to access the worksheets.

Each time the loop runs and a is incremented by one, this allows you to access the next sheet in the workbook.  This is why we needed to count how many sheets were in the workbook, so we would know when to tell the For loop to stop running because there were no more sheets.

So, we can access the worksheets from within the loop by using
ActiveWorkbook.Worksheets(index number)


Sheets(index number)

Remember that the variable a is being used as our index number in this case.

Using this method you can do anything you want with the corresponding worksheet.

And thats how you loop through all worksheets in a workbook in Excel!

Make sure to download the accompanying file for this tutorial so you can see the VBA/Macro code in action.

Downloadable Files: Excel File

Question? Ask it in our Excel Forum

Our Excel Courses

  • Skill Level: All Levels
  • |
  • 5 hours+

How to make a fully featured professional form in Excel that is unbreakable. This includes how to use the form to store, view, edit, and delete data from a data storage worksheet.

  • Skill Level: All Levels
  • |
  • 2 hours

Send Emails from Excel using VBA and Macros. This course starts from the Basics and builds up to more advanced examples with attaching workbooks, worksheets, PDF's, automatically sending emails, including a signature, error handling, increasing speed, and more.

Similar Content on TeachExcel
Get the Name of a Worksheet in Macros VBA in Excel
Tutorial: How to get the name of a worksheet in Excel using VBA and Macros and also how to store tha...
Excel Input Form with Macros and VBA
Tutorial: Forms Course How to make a data entry form in Excel using VBA and Macros - this allows yo...
Get User Submitted Data from a Prompt in Excel using VBA Macros
Tutorial: How to prompt a user for their input in Excel. There is a simple way to do this using VBA ...
Find the Next Blank Row with VBA Macros in Excel
Tutorial: Learn how to find the next empty cell in a range in Excel using VBA and Macros.  This me...
Loop Through an Array in Excel VBA Macros
Tutorial: I'll show you how to loop through an array in VBA and macros in Excel.  This is a fairly...
Copy Data or Formatting to Multiple Worksheets in Excel
Tutorial: Quickly copy all or parts of a single worksheet - data, formatting, or both - to multiple...