I have had a piece of code in operation for over 3 years. Suddenly on July 28th, 2016, it stopped working.
It is very simple and I hope it is an easy solve
The following lines of code will select
all sheets in the workbook the macro is called from:
Option Explicit
Public Sub SelectAllSheetsInThisFile()
Dim x As Long
Dim SheetstoSelect() As String
ReDim SheetstoSelect(1 To ThisWorkbook.Worksheets.Count)
For x = 1 To ThisWorkbook.Worksheets.Count
SheetstoSelect(x) = ThisWorkbook.Worksheets(x).Name
Next x
ThisWorkbook.Worksheets(SheetstoSelect).Select
End Sub
The following sub will just select
the two sheets you asked for in your original post:
Option Explicit
Public Sub SelectYourSheets()
Dim SheetstoSelect(1 To 2) As String
SheetstoSelect(1) = ThisWorkbook.Worksheets(1).Name
SheetstoSelect(2) = ThisWorkbook.Worksheets(2).Name
ThisWorkbook.Worksheets(SheetstoSelect).Select
End Sub
If you prefer to have it all in one line then you can also use split
to create an array on the fly like this:
ThisWorkbook.Worksheets(Split("Sheet1/Sheet3", "/")).Select
This line of code will select
two sheets with the names Sheet1
and Sheet3
. I chose the delimiter /
because this character cannot be used in a sheet's name.
Just on a side note: I agree with @BruceWayne. You should try to avoid using select
altogether (if possible).