Subscript Out of Range - Update Sheet Name (VBA)

BigBeachBananas

Active Member
Joined
Jul 13, 2021
Messages
450
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a workbook that has 10 tabs, some are hidden, but I don't think it matters for my purpose. On the "SQL" tab, I have a mapping of what I want the new sheet name to be (see A14:B24). I gave cell A15 a named range SheetStart for offsetting purposes in the code. You can ignore everything else on the sheet. When I run my macro, the debug.print sheetName is $200,00 which is correct, but on the next line when I assign "Option 1", it says subscript out of range. Why?
1694575870297.png



VBA Code:
Dim SheetStart As Range
        Dim wb2 As Workbook
        Dim sheetName As String
        Dim formattedCell As Range
        Set SheetStart = wb2.Sheets("SQL").Range("SheetStart")
        Set wb2 = ThisWorkbook
        
        wb2.Activate
        For i = 1 To 10
            ' Get the formatted value from the cell
            Set formattedCell = SheetStart.Offset(i - 1, 0)
            sheetName = formattedCell.Text
            ' Remove any invalid characters for sheet names
            sheetName = Replace(Replace(sheetName, "/", "_"), "\", "_")
            ' Check if the sheet name is too long (Excel limits sheet names to 31 characters)
            If Len(sheetName) > 31 Then
                sheetName = Trim(Left(sheetName, 31))
            End If
            Debug.Print sheetName
            Sheets(sheetName).Name = "Option " & i 'subscript error here
            Sheets("Summary").Range("Sheet" & i).Value = "Option " & i
            
        Next i
 

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"

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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