Loop Through Worksheets and Add Name to Combobox as long as Name Not in Array

breynolds0431

Active Member
Joined
Feb 15, 2013
Messages
303
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi. I have a userform with a combobox that populates worksheet names from the workbook. There are three conditions I want met before adding the worksheet name to the combobox. They are (1) the worksheet must be visible, (2) the worksheet cannot be included in an array of restricted workbook names, which is the arr variable, and (3) the workbook name cannot include a hyphen.

I have the below, but it's ignoring the arr list and adding them all multiple times to the combobox. I've tried a couple variations, but can't seem to wrap my head around why this isn't working...

Attempt 1
VBA Code:
Dim arr As Variant: arr = Array("START", "INFO", "CY", "CY_orig", "PY", "DATA VERIFY", "FLAGGED DUPLICATES")
Dim a As Variant, ws As Worksheet

For Each ws In ActiveWorkbook.Sheets
    If ws.Visible = True And InStr(ws.Name, "-") = 0 Then
        For Each a In arr
            If Not UCase(ws.Name) = a Then cboSheets.AddItem ws.Name
        Next a
    End If
Next ws

Attempt 2
VBA Code:
Dim arr As Variant: arr = Array("START", "INFO", "CY", "CY_orig", "PY", "DATA VERIFY", "FLAGGED DUPLICATES")
Dim a As Variant, ws As Worksheet

For Each ws In ActiveWorkbook.Sheets
    For Each a In arr
        If ws.Visible = True And Not UCase(ws.Name) = a And InStr(ws.Name, "-") = 0 Then cboSheets.AddItem ws.Name
    Next a
Next ws
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I figured out a solution with the help of some AI from ChatGPT.

VBA Code:
Dim arrExcludeSheets() As Variant
arrExcludeSheets = Array("START", "INFO", "CY", "CY_ORIG", "PY", "DATA VERIFY", "FLAGGED DUPLICATES")
Dim ws As Worksheet

For Each ws In ThisWorkbook.Sheets
    If IsError(Application.Match(ws.Name, arrExcludeSheets, 0)) Then
        If ws.Visible = xlSheetVisible And InStr(ws.Name, "-") = 0 Then
            cboSheets.AddItem ws.Name
        End If
    End If
Next ws
 
Upvote 0
Solution

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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