Looping Through Select Cases

G0DSL3FTHAND

New Member
Joined
Nov 20, 2018
Messages
1
Hello All,

I have a independent combobox and a dependent combobox, the dependent combobox depends on the information provided in the first independent combobox.

I know there is the select case to make fill the dependent combobox. Here is a brief overview what my code does.

I have two different user forms one for new materials and the second is for existing materials. The new material user form populates the storage sheet with the different materials. the material information comes in two forms a 4 digit number and a 2 digit number. the new material form determines if the 4 digit number already exists, and if it does it adds the 2 digit number under the column with the 4 digit column information. For Example the input is 5600 and 96, if 5600 already exist then 96 is added to the column for the information containing 5600, but if 5600 is new it becomes the header of a new column and 96 is added to the new 5600 column. the new material form also defines the name for the the new column as No.5600.

The problem I am having is that the independent list is always growing, and since the independent list is always growing so is the number of cases. So i tried to use a for loop in conjunction with select case to try and have the cases always growing. however, when the code is run the dependent combobox does not have any of the information under the name listed in the independent combobox.

Here is my code:
Private Sub UserForm_Initialize()
Dim z As Integer
Dim x As Integer
Dim ws As Worksheet
Dim wss As Worksheet
Set ws = Sheets("Storage")
Set wss = Sheets("Staging")
Me.myctl5.AddItem "Static Preload"
Me.myctl5.AddItem "Vertical Fatigue"
Me.myctl5.AddItem "Combination Test"


Me.myctl2.Clear
'Number of Columns with Numbers in them
ws.Range("B2").Formula = "=COUNT(A1:CC1)"
'z is the column location offset by one
z = ws.Range("B2").Value + 1


For x = 3 To z
Select Case myctl1
Case ws.Cells(1, z).Value
RowSource = "No." & ws.Cells(1, z).Value
End Select
Next

End Sub

Example table:
[TABLE="width: 500"]
<tbody>[TR]
[TD]4 digit[/TD]
[TD]4546[/TD]
[TD]5600[/TD]
[TD]5400[/TD]
[TD]4500[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4546[/TD]
[TD]12[/TD]
[TD]32[/TD]
[TD]54[/TD]
[TD]58
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5600[/TD]
[TD]15[/TD]
[TD]56[/TD]
[TD]65[/TD]
[TD]25[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5400[/TD]
[TD]32[/TD]
[TD]54[/TD]
[TD]22[/TD]
[TD]55[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4500[/TD]
[TD][/TD]
[TD]96[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi & welcome to MrExcel.
maybe something like
Code:
Private Sub ComboBox1_Click()
   Dim Fnd As Range
   Me.ComboBox2.Clear
   Set Fnd = Range("1:1").find(Me.ComboBox1.Value, , , xlWhole, , , , , False)
   If Fnd Is Nothing Then
      MsgBox Me.ComboBox1.Value & " not found"
   Else
      Me.ComboBox2.List = Range(Fnd.Offset(1), Fnd.End(xlDown)).Value
   End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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