Populate three different combobox on different WS with same VBA code

ron2k

Board Regular
Joined
Jan 28, 2008
Messages
139
Hi guys,

I have a combobox (COMBOBOX1) on each of three different worksheets ("Tab1", "Tab2", "Tab3"). Each combobox is populated by the same code. However, in my current settings I have the same code written three times in the "ThisWorkbook" variable. For example:

Code:
Private Sub Workbook_Open()
  With ThisWorkbook.Sheets("Tab1").ComboBox1
   '[MY IDENTICAL CODE]
  End With

  With ThisWorkbook.Sheets("Tab2").ComboBox1
   '[MY IDENTICAL CODE]
  End With

  With ThisWorkbook.Sheets("Tab3").ComboBox1
    '[MY IDENTICAL CODE]
   End With

This adds to the sluggishness of my workbook. Is there a way to combine my code to affect all three comboboxes at the same time?

Thanks,
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If this provides more info, this is what I essentially have:
Code:
Private Sub Workbook_Open()
  With ThisWorkbook.Worksheets("Tab1").ComboBox1
    Select Case Environ$("USERNAME")
    Case "ron2k_1", "frisbyt", "terryg", "tomcat"
      .AddItem ""
      .AddItem "Frig"
      .AddItem "Tel"
      .AddItem "Dull"
      .AddItem "Bug"
    Case "mguss", "qjang"
      .AddItem ""
      .AddItem "Dang"
      .AddItem "Conk"
    Case "errop"
      .AddItem ""
      .AddItem "Plast"
      .AddItem "teep"
    End Select
  End With

With ThisWorkbook.Worksheets("Tab2").ComboBox1
    Select Case Environ$("USERNAME")
    Case "ron2k_1", "frisbyt", "terryg", "tomcat"
      .AddItem ""
      .AddItem "Frig"
      .AddItem "Tel"
      .AddItem "Dull"
      .AddItem "Bug"
    Case "mguss", "qjang"
      .AddItem ""
      .AddItem "Dang"
      .AddItem "Conk"
    Case "errop"
      .AddItem ""
      .AddItem "Plast"
      .AddItem "teep"
    End Select
  End With

With ThisWorkbook.Worksheets("Tab3").ComboBox1
    Select Case Environ$("USERNAME")
    Case "ron2k_1", "frisbyt", "terryg", "tomcat"
      .AddItem ""
      .AddItem "Frig"
      .AddItem "Tel"
      .AddItem "Dull"
      .AddItem "Bug"
    Case "mguss", "qjang"
      .AddItem ""
      .AddItem "Dang"
      .AddItem "Conk"
    Case "errop"
      .AddItem ""
      .AddItem "Plast"
      .AddItem "teep"
    End Select
  End With
End Sub
The issue is that my list of usernames are pretty extensive, and anytime I need to delete one or add one or add items to a specific username I need to do it to all three comboboxes separately. Is there a condensed (and hopefully non-detrimental in loading time) way to do this.

Thanks again,
 
Upvote 0
How about ...
Code:
    Dim wks         As Worksheet
 
    For Each wks In Worksheets(Array("Tab1", "Tab2", "Tab3"))
        With wks.OLEObjects("ComboBox1").Object
            Select Case Environ$("USERNAME")
                Case "shg"
                    .AddItem ""
                    .AddItem "Frig"
                    .AddItem "Tel"
                    .AddItem "Dull"
                    .AddItem "Bug"
                Case Else
                    .AddItem ""
                    .AddItem "Dang"
                    .AddItem "Conk"
            End Select
        End With
    Next wks
 
Upvote 0
You're welcome.

You might also change all those AddItems to

Code:
.List =Array("", "Frig", "Tel", "Dull", "Bug")
 
Upvote 0
Even better!!! Thank you very much for your help. My code went from several pages long to probably just half-a-page.

Amazing what you can do when you know...
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

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