VBA Select all sheets in between

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
I have a workbook in which I need to select multiple sheets using VBA. The start sheet always has the same name and the end sheet always has the same name. However, there could be from 1 to 12 sheets in between with varying names (as well as some sheets after the end sheet or before the start sheet).

How can I select all the sheets in between start and end (including start and end) no matter how many are in the middle?

Thanks.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
Code:
Sheets(Evaluate("TRANSPOSE(ROW(" & Sheets([SIZE=3][COLOR=#ff0000][B]"Start"[/B][/COLOR][/SIZE]).Index + 1 & ":" & Sheets([SIZE=3][COLOR=#ff0000][B]"End"[/B][/COLOR][/SIZE]).Index - 1 & "))")).Select

Change the name of the sheets in bold
 
Upvote 0

RatExcel

Board Regular
Joined
Aug 24, 2014
Messages
222
Code:
Sub Select_Middle_Sheets()

If Sheets.Count < 3 Then Exit Sub

Dim myArr() As String
ReDim myArr(Sheets.Count - 3)

For i = 2 To Sheets.Count - 1
    myArr(j) = Sheets(i).Name
    j = j + 1
Next i

Sheets(myArr).Select
Sheets(2).Activate
    
End Sub
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
One more way...
Code:
Sub SelectSheetsBetweenStartAndEnd()
  Dim X As Long
  ActiveSheet.Select
  For X = Sheets("Start").Index + 1 To Sheets("End").Index - 1
    Sheets(X).Select False
  Next
End Sub
 
Last edited:
Upvote 0

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
One more way...
Code:
Sub SelectSheetsBetweenStartAndEnd()
  Dim X As Long
  ActiveSheet.Select
  For X = Sheets("Start").Index + 1 To Sheets("End").Index - 1
    Sheets(X).Select False
  Next
End Sub

That got me close. I made the mods below to capture both the start and end sheets and make sure it didn't matter which sheet the user was on when the macro was run. Thanks for the help!

Code:
Sub SelectSheetsBetweenStartAndEnd()
  Dim X As Long
  Sheets("Start").Select
  For X = Sheets("Start").Index + 1 To Sheets("End").Index
    Sheets(X).Select False
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,190,911
Messages
5,983,523
Members
439,848
Latest member
timmyo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top