Combo box - Select all worksheets except...

SeverusGrape

New Member
Joined
Dec 29, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi, first time poster here and rookie Excel user.

I have a very simple bit of code to select which worksheet will be active from a combo box.
I want to exclude specific worksheets from that combo box.

The idea, basically, is to have a different worksheet for each week of the year. This is for rostering. But rather than having a long string of worksheets to select from on the tab section, I want to hide the weekly sheets and select the work week from the combo box instead. That way I can have the other few worksheets I'll be using on this workbook to select from on the tab section without all the date worksheets cluttering it up.

Any help is appreciated. Thanks in advance.

My code for the combo box, to select a worksheet is...

VBA Code:
Private Sub cboStart_Change()

If cboStart <> "Choose Sheet" Then
Worksheets(cboStart.Value).Select
End If

cboStart.Value = "Choose Sheet"

End Sub

Private Sub Worksheet_Activate()

Dim sht As Worksheet
Me.cboStart.Clear
For Each sht In ThisWorkbook.Worksheets
Me.cboStart.AddItem sht.name

Next sht

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This example will exclude Sheet1 and Sheet2
VBA Code:
Private Sub cboStart_Change()

  If cboStart <> "Choose Sheet" Then
    Worksheets(cboStart.Value).Select
  End If

  cboStart.Value = "Choose Sheet"

End Sub
Private Sub Worksheet_Activate()
  Dim excludedWorksheets() As String
  Dim sht As Worksheet
  Dim wsVar As Boolean
  excludedWorksheets = Split("Sheet1,Sheet2", ",")

  Me.cboStart.Clear
  For Each sht In ThisWorkbook.Worksheets
    wsVar = False
    For i = 0 To UBound(excludedWorksheets)
      If sht.Name = excludedWorksheets(i) Then
        wsVar = True
      End If
      If Not wsVar Then
        Me.cboStart.AddItem sht.name
      End If
    Next
  Next
End Sub
 
Upvote 0
Hi SeverusGrape,

if the sheets you want to display start with a constant like Week you could use something like

VBA Code:
Private Sub cboStart_Change()

If cboStart <> "Choose Sheet" Then
  Worksheets(cboStart.Value).Select
End If

cboStart.Value = "Choose Sheet"

End Sub

Private Sub Worksheet_Activate()

Dim wks As Worksheet

Const cstrStartWks As String = "Week"   '<-- change to suit

Me.cboStart.Clear
For Each wks In ThisWorkbook.Worksheets
  If UCase(Left(wks.Name, Len(cstrStartWks))) = UCase(cstrStartWks) Then Me.cboStart.AddItem wks.Name
Next wks

End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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