Conditional sheet names macro?

gaftalik

Well-known Member
Joined
Feb 6, 2003
Messages
521
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

Any macro which can index all sheet names which are between two specified sheets ( ex: Sheet 6 and sheet 25) and in a desired range of cells?

I appreciate very much your help !
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
These routines can do that. You can either adjust the Destination Range and limiting sheet names in writeSheetsBetween.

Or select the cells where you want the names and enter the array formula =SheetsBetween("Sheet6","Sheet25")
Code:
Sub writeSheetsBetween()
    Dim outputArray As Variant
    Dim destinationRange As Range
    Set destinationRange = ThisWorkbook.Sheets("Sheet4").Range("a1")
    outputArray = SheetsBetween("Sheet1", "Sheet4")
    destinationRange.Resize(UBound(outputArray) - LBound(outputArray) + 1, 1) = Application.Transpose(outputArray)
End Sub

Function SheetsBetween(lowSheetName As String, highSheetName As String) As Variant
    Dim outRRay() As String, i As Long
    Dim lowSheetIndex As Long, highSheetIndex As Long

    lowSheetIndex = ThisWorkbook.Sheets(lowSheetName).Index
    highSheetIndex = ThisWorkbook.Sheets(highSheetName).Index
    If highSheetIndex < lowSheetIndex Then
        highSheetIndex = lowSheetIndex
        lowSheetIndex = ThisWorkbook.Sheets(highSheetName).Index
    End If

    If highSheetIndex <= lowSheetIndex + 1 Then
        ReDim outRRay(1 To 1)
        outRRay(1) = vbNullString
    Else
        ReDim outRRay(lowSheetIndex + 1 To highSheetIndex - 1)
        For i = lowSheetIndex + 1 To highSheetIndex - 1
            outRRay(i) = ThisWorkbook.Sheets(i).Name
        Next i
    End If

    SheetsBetween = outRRay
    If TypeName(Application.Caller) = "Range" Then
        If 1 < Application.Caller.Rows.Count Then
            SheetsBetween = Application.Transpose(outRRay)
        End If
    End If
End Function
Array formulas are entered with Ctrl-Shift-Enter (Cmd+Return for Mac)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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