subscript out of range into combobox inside sheet

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hello
I need fixing this error subscript out of range .
VBA Code:
Sheets(ComboBox1.Text).Visible = xlSheetVisible
by the way this code works for file but when implement in another shows the error .
VBA Code:
Private Sub ComboBox1_Change()
Dim sh As Worksheet

Application.ScreenUpdating = False
Sheets(ComboBox1.Text).Visible = xlSheetVisible

    For Each sh In ThisWorkbook.Sheets
        If sh.Name <> ComboBox1.Text And sh.Name <> "input" Then
            sh.Visible = xlSheetHidden
        End If
    Next sh
    
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
To get a "subscript out of range" error on the line Sheets(ComboBox1.Text).Visible = xlSheetVisible, the sheet name held in ComboBox1.Text would have to be an invalid sheet name.
Maybe add the line debug.print ComboBox1.Text & " - Length:" & len(ComboBox1.Text) and see what it shows in the immediate window
Then ensure that the sheet name exists and matches "exactly" ie check that neither the combo box text or the sheet name have leading or trailing blanks.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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