Select all worksheets in excel vba

This Excel VBA tutorial explains how to use Worksheet.Select Method to select a single worksheet or multiple worksheets.

When you click on a worksheet tab, the worksheet is highlighted.

To select multiple worksheets, you can hold down Ctrl and then left click the mouse on each worksheet tab.

Excel Assign Page Number 01

To select all worksheets at once, right click on one of the sheet, and then click on Select All Sheets

Excel select multiple worksheets 01

One practical use of selecting multiple worksheets is to print selected worksheets.

In this tutorial, I will explain how to perform the same tasks in the above scenarios using Excel VBA Worksheet.Select Method.

Excel VBA Worksheet.Select Method

In Excel VBA, it is not necessary to select worksheets in order to run a Macro on selected worksheets, because you can use VBA to loop through worksheets with specific name.

Syntax of Worksheet.Select Method

Worksheet.Select(Replace)
Name Required/Optional Data Type Description
Replace Optional Variant (used only with sheets). True to replace the current selection with the specified object. False to extend the current selection to include any previously selected objects and the specified object.

Example 1 – Select a single worksheet

To select Sheet1 only

Sheets("Sheet1").Select

Example 2 – Select multiple worksheets

To select Sheet1 and Sheet2, use the False Property in Sheet2

you can also add the False argument for the first Worksheet

Sheets("Sheet1").Select False
Sheets("Sheet2").Select False

Excel VBA Worksheet.Select Method to select multiple worksheets 01

Example 3 – Select all worksheets in the workbook

The below example selects all worksheets in current workbook

Public Sub selectAllWS()
   For Each ws In ThisWorkbook.Sheets
      ws.Select flase
   Next
End Sub

Excel VBA Worksheet.Select Method to select worksheets 02

After you have selected all worksheets, you can deselect them by selecting anyone of the worksheet. To avoid specifying which worksheet, I use ActiveSheet in the below example.

In multiple selection, ActiveSheet refers to the first selected worksheet.

Public Sub deselectWS()
   ActiveSheet.Select
End Sub

You can also select multiple worksheets using Array.

Outbound References

https://msdn.microsoft.com/en-us/library/office/ff194988.aspx

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.

Содержание

  1. VBA Select Sheet, Activate Sheet, and Get Activesheet
  2. ActiveSheet
  3. Activate Worksheet (Setting the ActiveSheet)
  4. ActiveSheet Name
  5. Selected Sheets vs ActiveSheet
  6. Select Worksheet
  7. Select Worksheet by Tab Name
  8. Select Worksheet by Index Number
  9. Select Worksheet With VBA Code Name
  10. Select Current Worksheet
  11. More Activate / Select Sheet Examples
  12. Set ActiveSheet to Variable
  13. Change ActiveSheet Name
  14. With ActiveSheet
  15. Loop Through Selected Sheets
  16. GoTo Next Sheet
  17. VBA Coding Made Easy
  18. VBA Code Examples Add-in
  19. Excel VBA — Selecting Worksheets
  20. You are here
  21. Excel VBA — Selecting Worksheets
  22. Selecting a single Worksheet
  23. Selecting all Worksheets
  24. Selecting the Last Worksheet in a workbook
  25. VBA Sheets – The Ultimate Guide
  26. Sheets Vs. Worksheets
  27. Referencing Sheets
  28. ActiveSheet
  29. Sheet Name
  30. Sheet Index Number
  31. Sheet Index Number – Last Sheet in Workbook
  32. Sheet “Code Name”
  33. VBA Coding Made Easy
  34. Referencing Sheets in Other Workbooks
  35. Activate vs. Select Sheet
  36. Activate a Sheet
  37. Select a Sheet
  38. Select Multiple Sheets
  39. Worksheet Variable
  40. Loop Through All Sheets in Workbook
  41. Worksheet Protection
  42. Workbook Protection
  43. Worksheet Protection
  44. Protect Worksheet
  45. Unprotect Worksheet
  46. Worksheet Visible Property
  47. Unhide Worksheet
  48. Hide Worksheet
  49. Very Hide Worksheet
  50. Worksheet-Level Events
  51. Worksheet Activate Event
  52. Worksheet Change Event
  53. Worksheet Cheat Sheet
  54. VBA Worksheets Cheatsheet
  55. VBA Code Examples Add-in

VBA Select Sheet, Activate Sheet, and Get Activesheet

In this Article

This article will discuss the ActiveSheet object in VBA. It will also discuss how to activate, select, and go to Worksheets (& much more). Read our full VBA Worksheets Guide for more information about working with worksheets in VBA.

ActiveSheet

In VBA, ActiveSheet refers to the currently active Worksheet. Only one Sheet may be active at a time.

Activate Worksheet (Setting the ActiveSheet)

To set the ActiveSheet use Worksheet.Activate:

The Activate Sheet command will actually “go to” the sheet, changing the visible Sheet.

The above example uses the Sheet (Tab) name. Instead you can use the VBA code name for the worksheet:

ActiveSheet Name

To get the ActiveSheet Name:

Selected Sheets vs ActiveSheet

At any point in time, only one Sheet can be the ActiveSheet. However, multiple Worksheets can be selected at once.

When multiple Worksheets are selected only the “top-most” Worksheet is considered active (the ActiveSheet).

Select Worksheet

If you would like to select a worksheet instead of activating it. Use .Select instead.

Select Worksheet by Tab Name

This selects a Worksheet based on it’s Sheet Tab Name

Select Worksheet by Index Number

This selects a Worksheet based on it’s position relative to other tabs

Select Worksheet With VBA Code Name

Selecting worksheets by code name can prevent errors caused by worksheet name changes.

Select Current Worksheet

To select the current Worksheet, use the ActiveSheet object:

More Activate / Select Sheet Examples

Set ActiveSheet to Variable

This will assign the ActiveSheet to a Worksheet Object Variable.

Change ActiveSheet Name

With ActiveSheet

Using the With Statement allows you to streamline your code when working with objects (such as Sheets or ActiveSheet).

Notice how you don’t need to repeat “ActiveSheet” before each line of code. This can be a huge time saver when working with a long list of commands.

Loop Through Selected Sheets

The following macro will Loop through all selected sheets, displaying their names.

GoTo Next Sheet

This code will go to the next Sheet. If the ActiveSheet is the last Sheet, then it will go to the first Sheet in the Workbook.

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!

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.

Источник

Excel VBA — Selecting Worksheets

You are here

Excel VBA — Selecting Worksheets

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

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

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

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:

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

Selecting the Last Worksheet in a workbook

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

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.

Источник

VBA Sheets – The Ultimate Guide

In this Article

This is the ultimate guide to working with Excel Sheets / Worksheets in VBA.

At the bottom of this guide, we’ve created a cheat sheet of common commands for working with sheets.

Sheets Vs. Worksheets

There are two ways to reference Sheets using VBA. The first is with the Sheets object:

The other is with the Worksheets object:

99% of the time, these two objects are identical. In fact, if you’ve searched online for VBA code examples, you’ve probably seen both objects used. Here is the difference:

The Sheets Collection contains Worksheets AND Chart Sheets.

So use Sheets if you want to include regular Worksheets AND Chart Sheets. Use Worksheets if you want to exclude Chart Sheets. For the rest of this guide we will use Sheets and Worksheets interchangeably.

Referencing Sheets

There are several different ways to reference Sheets:

  • ActiveSheet
  • Sheet Tab Name
  • Sheet Index Number
  • Sheet Code Name

ActiveSheet

The ActiveSheet is the Sheet that’s currently active. In other words, if you paused your code and looked at Excel, it’s the sheet that is visible. The below code example will display a MessageBox with the ActiveSheet name.

Sheet Name

You are probably most familiar with referencing Sheets by their Tab Name:

This is the sheet name that’s visible to Excel users. Enter it into the sheets object, as a string of text, surrounded by quotations.

Sheet Index Number

The Sheet Index number is the sheet position in the workbook. 1 is the first sheet. 2 is the second sheet etc.:

Sheet Index Number – Last Sheet in Workbook

To reference the last Sheet in the workbook, use Sheets.Count to get the last Index Number and activate that sheet:

Sheet “Code Name”

The Sheet Code Name is it’s Object name in VBA:

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!

Referencing Sheets in Other Workbooks

It’s also easy to reference Sheets in other Workbooks. To do so, you need to use the Workbooks Object:

Important: The Workbook must be open before you can reference its Sheets.

Activate vs. Select Sheet

In another article we discuss everything about activating and selecting sheets. The short version is this:

When you Activate a Sheet it becomes the ActiveSheet. This is the sheet you would see if you looked at your Excel program. Only one sheet may be activate at a time.

Activate a Sheet

When you select a Sheet, it also becomes the ActiveSheet. However, you can select multiple sheets at once. When multiple sheets are selected at once, the “top” sheet is the ActiveSheet. However, you can toggle the ActiveSheet within selected sheets.

Select a Sheet

Select Multiple Sheets

Use an array to select multiple sheets at once:

Worksheet Variable

Assigning a worksheet to an object variable allows you to reference the worksheet by it’s variable name. This can save a lot of typing and make your code easier to read. There are also many other reasons you might want to use variables.

To declare a worksheet variable:

Assign a worksheet to a variable:

Now you can reference the worksheet variable in your code:

Loop Through All Sheets in Workbook

Worksheet variables are useful when you want to loop through all the worksheets in a workbook. The easiest way to do this is:

This code will loop through all worksheets in the workbook, displaying each worksheet name in a message box. Looping through all the sheets in a workbook is very useful when locking / unlocking or hiding / unhiding multiple worksheets at once.

Worksheet Protection

Workbook Protection

Workbook protection locks the workbook from structural changes like adding, deleting, moving, or hiding worksheets.

You can turn on workbook protection using VBA:

or disable workbook protection:

Note: You can also protect / unprotect without a password by omitting the Password argument:

Worksheet Protection

Worksheet-level protection prevents changes to individual worksheets.

Protect Worksheet

Unprotect Worksheet

There are a variety of options when protecting worksheets (allow formatting changes, allow user to insert rows, etc.) We recommend using the Macro Recorder to record your desired settings.

We discuss worksheet protection in more detail here.

Worksheet Visible Property

You might already know that worksheets can be hidden:

There are actually three worksheet visibility settings: Visible, Hidden, and VeryHidden. Hidden sheets can be unhidden by any regular Excel user – by right-clicking in the worksheet tab area (shown above). VeryHidden sheets can only be unhidden with VBA code or from within the VBA Editor. Use the following code examples to hide / unhide worksheets:

Unhide Worksheet

Hide Worksheet

Very Hide Worksheet

Worksheet-Level Events

Events are triggers that can cause “Event Procedures” to run. For example, you can cause code to run every time any cell on a worksheet is changed or when a worksheet is activated.

Worksheet event procedures must be placed in a worksheet module:

There are numerous worksheet events. To see a complete list, go to a worksheet module , select “Worksheet” from the first drop-down. Then selecting an event procedure from the second drop-down to insert it into the module.

Worksheet Activate Event

Worksheet activate events run each time the worksheet is opened.

This code will select cell A1 (resetting the view area to the top-left of the worksheet) each time the worksheet is opened.

Worksheet Change Event

Worksheet change events run whenever a cell value is changed on the worksheet. Read our tutorial about Worksheet Change Events for more information.

Worksheet Cheat Sheet

Below you will find a cheat sheet containing common code examples for working with sheets in VBA

VBA Worksheets Cheatsheet

VBA worksheets Cheatsheet

Description Code Example
Referencing and Activating Sheets
Tab Name Sheets(«Input»).Activate
VBA Code Name Sheet1.Activate
Index Position Sheets(1).Activate
Select Sheet
Select Sheet Sheets(«Input»).Select
Set to Variable Dim ws as Worksheet
Set ws = ActiveSheet
Name / Rename ActiveSheet.Name = «NewName»
Next Sheet ActiveSheet.Next.Activate
Loop Through all Sheets Dim ws as Worksheet

For each ws in Worksheets
Msgbox ws.name
Next ws Loop Through Selected Sheets Dim ws As Worksheet

For Each ws In ActiveWindow.SelectedSheets
MsgBox ws.Name
Next ws Get ActiveSheet MsgBox ActiveSheet.Name Add Sheet Sheets.Add Add Sheet and Name Sheets.Add.Name = «NewSheet» Add Sheet With Name From Cell Sheets.Add.Name = range(«a3»).value Add Sheet After Another Sheets.Add After:=Sheets(«Input») Add Sheet After and Name Sheets.Add(After:=Sheets(«Input»)).Name = «NewSheet» Add Sheet Before and Name Sheets.Add(Before:=Sheets(«Input»)).Name = «NewSheet» Add Sheet to End of Workbook Sheets.Add After:=Sheets(Sheets.Count) Add Sheet to Beginning of Workbook Sheets.Add(Before:=Sheets(1)).Name = «FirstSheet» Add Sheet to Variable Dim ws As Worksheet
Set ws = Sheets.Add Copy Worksheets Move Sheet to End of Workbook Sheets(«Sheet1»).Move After:=Sheets(Sheets.Count) To New Workbook Sheets(«Sheet1»).Copy Selected Sheets To New Workbook ActiveWindow.SelectedSheets.Copy Before Another Sheet Sheets(«Sheet1»).Copy Before:=Sheets(«Sheet2») Before First Sheet Sheets(«Sheet1»).Copy Before:=Sheets(1) After Last Sheet Sheets(«Sheet1»).Copy After:=Sheets(Sheets.Count) Copy and Name Sheets(«Sheet1»).Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = «LastSheet» Copy and Name From Cell Value Sheets(«Sheet1»).Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = Range(«A1»).Value To Another Workbook Sheets(«Sheet1»).Copy Before:=Workbooks(«Example.xlsm»).Sheets(1) Hide / Unhide Sheets Hide Sheet Sheets(«Sheet1»).visible = False
or
Sheets(«Sheet1»).visible = xlSheetHidden Unhide Sheet Sheets(«Sheet1»).Visible = True
or
Sheets(«Sheet1»).Visible = xlSheetVisible Very Hide Sheet Sheets(“Sheet1”).Visible = xlSheetVeryHidden Delete or Clear Sheets Delete Sheet Sheets(«Sheet1»).Delete Delete Sheet (Error Handling) On Error Resume Next
Sheets(«Sheet1»).Delete
On Error GoTo 0 Delete Sheet (No Prompt) Application.DisplayAlerts = False
Sheets(«Sheet1»).Delete
Application.DisplayAlerts = True Clear Sheet Sheets(«Sheet1»).Cells.Clear Clear Sheet Contents Only Sheets(«Sheet1»).Cells.ClearContents Clear Sheet UsedRange Sheets(«Sheet1»).UsedRange.Clear Protect or Unprotect Sheets Unprotect (No Password) Sheets(«Sheet1»).Unprotect Unprotect (Password) Sheets(«Sheet1»).Unprotect «Password» Protect (No Password) Sheets(«Sheet1»).Protect Protect (Password) Sheets(«Sheet1»).Protect «Password» Protect but Allow VBA Access Sheets(«Sheet1»).Protect UserInterfaceOnly:=True Unprotect All Sheets Dim ws As Worksheet

For Each ws In Worksheets
ws.Unprotect «password»
Next ws

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.

Источник

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

  • #2

Welcome to the Board!<pre>
Sheets.Select</pre>

that should do it.

EDIT:: Should’ve tested that with some hidden sheets really shouldn’t I? Nate Oliver’s solution is the way to go.
_________________<font color = green> Mark O’Brien </font>

Columbus Ohio Celtic Supporters Club
This message was edited by Mark O’Brien on 2002-12-19 10:48

  • #3

ActiveWorkbook.Sheets.Select

  • #4

Howdy Oliver (sharp name!), welcome to the board. How’s about the following:

<pre>
Sub gram_em()
Dim ws As Worksheet
For Each ws In Sheets
If ws.Visible Then ws.Select (False)
Next
End Sub</pre>

  • #5

ta Nate it was that ****ing false thing i’d missed

:)

  • #6

Hi Nate,

What the use for «(false)» in that syntax?
If i using that syntax, error message appear

«run time error ‘1004’
application-defined or object-defined error»

But when i delete the (false), all the syntax work like charm

  • #7

It was 2002 when Nate wrote that. I don’t think it likely that he is still subscribed to this thread.

  • #8

Hi Nate,

What the use for «(false)» in that syntax?
If i using that syntax, error message appear

«run time error ‘1004’
application-defined or object-defined error»

But when i delete the (false), all the syntax work like charm

I believe the (false) flag tells Excel not to replace whatever’s currently selected with the object you’re selecting (I think it only works with worksheets; not sure it works with ranges, etc.). (true) would tell it to select the sheet as the only sheet selected; (false) tells it to keep selecting.

  • #9

I know it is an old thread, but it was usefully to me….
Remove the brackets () around FALSE
:eek:

Понравилась статья? Поделить с друзьями:
  • Select all worksheet in excel
  • Select cases in excel
  • Select all word mac
  • Select case формула excel
  • Select all visible cells in excel