Hiding and revealing the sheet with the sheet list

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
80
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello
In an Excel file that has several sheets, there is a ComboBox in each of the sheets.

I created a sheet list with the code below
VBA Code:
Private Sub Worksheet_Activate()
ComboBox1.Clear
 
For wsheet = 1 To Sheets.Count
   ComboBox1.AddItem Sheets(wsheet).Name
Next
 
End Sub

By selecting the name of the sheet from the ComboBox, we go to the desired sheet
VBA Code:
Private Sub ComboBox1_Change()

 If Not ComboBox1.Text = "" Then
    Sheets(ComboBox1.Text).Select
 End If

 End Sub
How can I hide other sheets by selecting the name of the sheet and going to the desired sheet?
For this, I used the following code, but it did not work correctly

VBA Code:
Private Sub ComboBox1_Change()
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Visible = False
 
    If Not ComboBox1.Text = "" Then
       Sheets(ComboBox1.Text).Visible = True
       Sheets(ComboBox1.Text).Select
   End If

End Sub

Please help me how to do this
 

Attachments

  • 33.png
    33.png
    34.7 KB · Views: 8
  • 34.png
    34.png
    21.6 KB · Views: 9

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
How about?
VBA Code:
Private Sub ComboBox1_Change()
Dim ws As Worksheet
If ComboBox1.Text = "" Then Exit Sub ' Do nothing if select "blank"

' Hide all sheets except the desired sheet
For Each ws In ThisWorkbook.Sheets
    If ws.Name <> ComboBox1.Text Then
        ws.Visible = xlSheetHidden
    End If
Next ws

' Show and select the desired sheet
 With Sheets(ComboBox1.Text)
        .Visible = xlSheetVisible
        .Select
End With
End Sub
 
Upvote 0
How about?
VBA Code:
Private Sub ComboBox1_Change()
Dim ws As Worksheet
If ComboBox1.Text = "" Then Exit Sub ' Do nothing if select "blank"

' Hide all sheets except the desired sheet
For Each ws In ThisWorkbook.Sheets
    If ws.Name <> ComboBox1.Text Then
        ws.Visible = xlSheetHidden
    End If
Next ws

' Show and select the desired sheet
 With Sheets(ComboBox1.Text)
        .Visible = xlSheetVisible
        .Select
End With
End Sub
Thank you for your guide
Your code worked correctly in the first step, by selecting the desired sheet, it was moved to it and other sheets were hidden
But when we try to move to another sheet, we encounter the following error and the program gives an error

Microsoft Visual Basic

Run-time error '1004':

Method 'Visible' of object '_Worksheet' failed

001.png
 
Upvote 0
It may be becuase when the code is run the second time and the combobox value has changed it will be trying to hide all of the sheets in the workbook which can't be done. Try swapping the two code blocks around so it makes the desired tab visible before it hides the ones that are not needed.
 
Upvote 0
It may be becuase when the code is run the second time and the combobox value has changed it will be trying to hide all of the sheets in the workbook which can't be done. Try swapping the two code blocks around so it makes the desired tab visible before it hides the ones that are not needed.
Can you help me with more examples?
 
Upvote 0
Can you help me with more examples?
Just swapping the two blocks of code:
VBA Code:
Private Sub ComboBox1_Change()
    Dim ws As Worksheet
    If ComboBox1.Text = "" Then Exit Sub ' Do nothing if select "blank"
    
    ' Show and select the desired sheet
    With Sheets(ComboBox1.Text)
            .Visible = xlSheetVisible
            .Select
    End With
    
    ' Hide all sheets except the desired sheet
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> ComboBox1.Text Then
            ws.Visible = xlSheetHidden
        End If
    Next ws
End Sub
 
Upvote 0
Just swapping the two blocks of code:
VBA Code:
Private Sub ComboBox1_Change()
    Dim ws As Worksheet
    If ComboBox1.Text = "" Then Exit Sub ' Do nothing if select "blank"
   
    ' Show and select the desired sheet
    With Sheets(ComboBox1.Text)
            .Visible = xlSheetVisible
            .Select
    End With
   
    ' Hide all sheets except the desired sheet
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> ComboBox1.Text Then
            ws.Visible = xlSheetHidden
        End If
    Next ws
End Sub
Thank you for your beautiful answer
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,575
Members
452,652
Latest member
eduedu

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