Finding Worksheet Names Located Between Two Worksheets?

Duvnjak

New Member
Joined
Aug 14, 2003
Messages
27
Does anyone know of an effective way in VBA to grab all the worksheet names located within two different worksheets?

i.e. Start,1,2,3,4,5,End

I want to grab the names of 1,2,3,4,5.

Thanks in advance!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This code should do what you want. its a bit meandering because I can't think of a cleaner code to do it.

This code assumes that (1) you have a named range called "startsheet" and "endsheet" where you enter the Start Sheet and End Sheet names you want to find sheets betwee and (2) that your output will be put on a sheet named "sheet1" beginning in Cell "A1".

Code:
Sub FindSheetNames()

Dim startsheet As Variant
Dim endsheet As Variant
Dim startsheetnum As Long
Dim endsheetnum As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim SheetCount As Long

startsheet = Range("startsheet").Value
endsheet = Range("endsheet").Value
SheetCount = ActiveWorkbook.Sheets.Count

For i = 1 To SheetCount

    If Sheets(i).Name = startsheet Then
    
        startsheetnum = i
    
    ElseIf Sheets(i).Name = endsheet Then
    
        endsheetnum = i
    
    End If

Next i

k = 1

For j = startsheetnum + 1 To endsheetnum - 1

    Sheets("Sheet1").Select
    Cells(k, 1) = Sheets(j).Name
    
    k = k + 1

Next j

End Sub
 
Upvote 0
Voyla... (voila)

Swap "Start" and "End" with the name of your start and end sheet.
Stores sheet names in wSheetNames() string array variable to use with VBA code.

So...
wSheetNames(0) = "1"
wSheetNames(1) = "2" etc.

UBound(wSheetNames) = total # of sheets

Code:
Sub GimmeSheetNames()
Dim wSheetNames() As String
Dim betweenTheSheets As Integer
Dim pullNames As Boolean
 
betweenTheSheets = -1
 
For Each wSheet In ThisWorkbook.Worksheets
If wSheet.Name = "End" Then
pullNames = False
End If
 
If pullNames Then
betweenTheSheets = betweenTheSheets + 1
ReDim Preserve wSheetNames(0 To betweenTheSheets)
wSheetNames(betweenTheSheets) = wSheet.Name
' MsgBox Prompt:="wSheetNames(" & betweenTheSheets & ") = " & wSheetNames(betweenTheSheets)
End If
 
If wSheet.Name = "Start" Then
pullNames = True
End If
Next wSheet
End Sub

Uncomment MsgBox prompt to check name selection

:biggrin:
 
Last edited:
Upvote 0
Try this.
Code:
Option Explicit
 
Sub GetWSNames()
Dim ws As Worksheet
Dim arrWSnms()
Dim I As Long
 
    For Each ws In Worksheets

        Select Case ws.Name
            Case "Start", "End"
                ' do nothing
            Case Else
                ReDim Preserve arrWSnms(I)
                arrWSnms(I) = ws.Name
                I = I + 1
        End Select
                
    Next ws
    
    MsgBox Join(arrWSnms, ",")
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,526
Messages
6,125,328
Members
449,218
Latest member
Excel Master

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
Back
Top