Vba excel all sheets in workbook

Summary

This article contains a Microsoft Visual Basic for Applications macro (Sub procedure) that loops through all the worksheets in the active workbook. This macro also displays the name of each worksheet.

More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To try the sample macro, follow these steps:

  1. Type the following macro code into a new module sheet.

          Sub WorksheetLoop()

    Dim WS_Count As Integer
    Dim I As Integer

    ' Set WS_Count equal to the number of worksheets in the active
    ' workbook.
    WS_Count = ActiveWorkbook.Worksheets.Count

    ' Begin the loop.
    For I = 1 To WS_Count

    ' Insert your code here.
    ' The following line shows how to reference a sheet within
    ' the loop by displaying the worksheet name in a dialog box.
    MsgBox ActiveWorkbook.Worksheets(I).Name

    Next I

    End Sub

  2. To run the macro, position the insertion point in the line that reads «Sub WorksheetLoop(),» and press F5.

The macro will loop through the workbook and display a message box with a different worksheet name each time it runs through the loop. Note that this macro will only display worksheet names; it will not display the names of other types of sheets in the workbook.

You can also loop through all of the worksheets in the workbook by using a ‘For Each’ loop.

  1. Enter the following macro code into a new module sheet.

          Sub WorksheetLoop2()

    ' Declare Current as a worksheet object variable.
    Dim Current As Worksheet

    ' Loop through all of the worksheets in the active workbook.
    For Each Current In Worksheets

    ' Insert your code here.
    ' This line displays the worksheet name in a message box.
    MsgBox Current.Name
    Next

    End Sub

  2. To run the macro, position the insertion point in the line that reads «Sub WorksheetLoop2(),» and press F5.

This macro works identically to the WorksheetLoop macro, except that it uses a different type of loop to process all of the worksheets in the active workbook.

References

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

163435 VBA: Programming Resources for Visual Basic for Applications

226118 OFF2000: Programming Resources for Visual Basic for Applications

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Return to VBA Code Examples

The following macro loops through every sheet in a workbook and writes the tab name of each sheet sequentially to a sheet you choose. This could be handy for a quick list of every sheet in a workbook with many sheets.

List all Worksheets in a Workbook

To use the macro just replace the word Sheet1(it appears twice) in the code with the tab name where you would like the results. Make sure there isn’t any important information on the output tab because it clears the data their before writing to it.

Sub ListSheets()

Dim ws As Worksheet
Dim x As Integer

x = 1

Sheets("Sheet1").Range("A:A").Clear

For Each ws In Worksheets
     Sheets("Sheet1").Cells(x, 1) = ws.Name
     x = x + 1
Next ws

End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

The workbook has over 50 worksheets and I’m working through an old macro that will help me amend the same range within each sheet. However they have hardcoded the sheets.select statement.

How do you write: Sheets(Array("Sheet1", "Bob", "1953", etc, etc, etc)).Select so that all the sheets are selected — from first to last, no matter what name someone has given them?

Toddleson's user avatar

Toddleson

4,2271 gold badge6 silver badges26 bronze badges

asked Jul 16, 2010 at 14:11

RocketGoal's user avatar

1

ThisWorkbook.Sheets.Select

answered Jul 16, 2010 at 14:21

Lunatik's user avatar

LunatikLunatik

3,8286 gold badges37 silver badges52 bronze badges

1

When writing VBA, there are a number of occasions when you will have to select a single or group of worksheets. In this blog, we will explore some of the ways we can do this.

Selecting a single Worksheet
Selecting all Worksheets
Selecting the last sheet

Selecting a single Worksheet

The first method of selecting a sheet, uses the sheets name. The example below would select the sheet called Sheet 1.

Sub SelectSingleSheet()

           Sheets(«Sheet1»).Select

    End Sub

The second method uses the sheets position in the workbook. The example below would select the first sheet in the workbook.

Sub SelectSingleSheet2()

        Sheets(«1»).Select

    End Sub

Both methods have negatives, and so you need to be aware, that if selecting a sheet with it’s name, the name must not be changed. If selecting a sheet by position, then the sheet must not be moved. 

Selecting all Worksheets

Selecting all the sheets in the workbook can be done using the following code:

Sub SelectAllSheets()

        Sheets.Select

    End Sub

Some examples of why you may need to select all the worksheets at once include:

  • Print Setup for the entire workbook
  • Zoom in/out in all worksheets
  • Colour all of the Worksheet tabs
  • To run a Macro/VBA on all Worksheets

etc..

Selecting the Last Worksheet in a workbook

You are able to select the last sheet in a workbook, by using the following code:

Sub LastWorksheet()

    Sheets(Sheets.Count).Select

    End Sub

By using sheets.count, you are counting how many sheets there are in the workbook. For example, If there were 3 sheets in your workbook, then you would be selecting the third sheet (from the left), and so the last sheet.

To learn more about Excel VBA, join us on our Introduction to Excel Macros/VBA Course.

If you have an Excel workbook that has hundreds of worksheets, and now you want to get a list of all the worksheet names, you can refer to this article. Here we will share 3 simple methods with you.

Sometimes, you may be required to generate a list of all worksheet names in an Excel workbook. If there are only few sheets, you can just use the Method 1 to list the sheet names manually. However, in the case that the Excel workbook contains a great number of worksheets, you had better use the latter 2 methods, which are much more efficient.

Method 1: Get List Manually

  1. First off, open the specific Excel workbook.
  2. Then, double click on a sheet’s name in sheet list at the bottom.
  3. Next, press “Ctrl + C” to copy the name.Copy Sheet Name
  4. Later, create a text file.
  5. Then, press “Ctrl + V” to paste the sheet name.Paste Sheet Name
  6. Now, in this way, you can copy each sheet’s name to the text file one by one.

Method 2: List with Formula

  1. At the outset, turn to “Formulas” tab and click the “Name Manager” button.
  2. Next, in popup window, click “New”.Name Manager
  3. In the subsequent dialog box, enter “ListSheets” in the “Name” field.
  4. Later, in the “Refers to” field, input the following formula:
=REPLACE(GET.WORKBOOK(1),1,FIND("]",GET.WORKBOOK(1)),"")

Customize New Name

  1. After that, click “OK” and “Close” to save this formula.
  2. Next, create a new worksheet in the current workbook.
  3. Then, enter “1” in Cell A1 and “2” in Cell A2.
  4. Afterwards, select the two cells and drag them down to input 2,3,4,5, etc. in Column A.Enter Sequential Numbers
  5. Later, put the following formula in Cell B1.
=INDEX(ListSheets,A1)

Enter Formula in Cell B1

  1. At once, the first sheet name will be input in Cell B1.
  2. Finally, just copy the formula down until you see the “#REF!” error.Copy Formula Down to List Sheet Names

Method 3: List via Excel VBA

  1. For a start, trigger Excel VBA editor according to “How to Run VBA Code in Your Excel“.
  2. Then, put the following code into a module or project.
Sub ListSheetNamesInNewWorkbook()
    Dim objNewWorkbook As Workbook
    Dim objNewWorksheet As Worksheet

    Set objNewWorkbook = Excel.Application.Workbooks.Add
    Set objNewWorksheet = objNewWorkbook.Sheets(1)

    For i = 1 To ThisWorkbook.Sheets.Count
        objNewWorksheet.Cells(i, 1) = i
        objNewWorksheet.Cells(i, 2) = ThisWorkbook.Sheets(i).Name
    Next i

    With objNewWorksheet
         .Rows(1).Insert
         .Cells(1, 1) = "INDEX"
         .Cells(1, 1).Font.Bold = True
         .Cells(1, 2) = "NAME"
         .Cells(1, 2).Font.Bold = True
         .Columns("A:B").AutoFit
    End With
End Sub

VBA Code - List Sheet Names

  1. Later, press “F5” to run this macro right now.
  2. At once, a new Excel workbook will show up, in which you can see the list of worksheet names of the source Excel workbook.Listed Sheet Names in New Excel Workbook

Comparison

Advantages Disadvantages
Method 1 Easy to operate Too troublesome if there are a lot of worksheets
Method 2 Easy to operate Demands you to type the index first
Method 3 Quick and convenient Users should beware of the external malicious macros
Easy even for VBA newbies

Excel Gets Corrupted

MS Excel is known to crash from time to time, thereby damaging the current files on saving. Therefore, it’s highly recommended to get hold of an external powerful Excel repair tool, such as DataNumen Outlook Repair. It’s because that self-recovery feature in Excel is proven to fail frequently.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including sql fix and outlook repair software products. For more information visit www.datanumen.com

Понравилась статья? Поделить с друзьями:
  • Vba excel all pdf
  • Vba excel all opened
  • Vba excel all files in folder
  • Vba excel ado примеры
  • Vba excel address ячейки