Combobox troubles

g-man97

New Member
Joined
Sep 30, 2004
Messages
24
I am trying to populate a combobox on a userform. It's my 1st atempt and needless to say it's not going so well.

I've read several of the posts and thought I could handle it.

I put the userform_initalize sub into the userform and then did
load SomeName (the userform name) followed by SomeName.Show in the worksheet module attached to a command button.

When i step through the code i can see it going through my list but nothing shows up in the combobox. Grrrrr! BTW, the list i'm using is in column F on Sheet2, starting in row 5 and then in every other row, (still in same column), e.g. f5, f7, f9, f1...and so on.


Any help or suggestions would be appreciated.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The .AddItem Method will add an item to a combobox list. Load will load the userform into memory without showing it.
 
Upvote 0
TazGuy, here's my code. It is in a userform module.

Private Sub UserForm_Initialize()
Dim rng As Range
Dim LastRow As Long
Dim I As Long
Sheets("ScenariosTbl").Activate
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
For I = 5 To LastRow - 4
Set rng = ActiveSheet.Range("F" & I)
MsgBox "usrfrm initialize, listvalue is " & rng
If rng.Offset(0, 1) = "Active" Then
ScenariosMenu.ComboBox1.AddItem rng.Value
End If
Next I
End Sub

The msgbox (just for testing) shows the values but none of them show up in the combobox on the userform. Also, I need to use every other row, i.e. f5, f7, f9, etc. As it is now, this goes through every row and there is only data in every other, so I'd prefer not to have a bunch of blanks in my list.

Is this code in the right module? I am launching the userform from a worksheet module with load FormName followed by FormName.Show. Are these in the right module?

Finally, how do I capture the value of the comobox? Would OffName = Me.ComboBox1.Caption do the trick?

While I'm at it, what goes in the ComboBox1_Change sub?

Sorry to be so dense. Thanks again everyone!! You're lifesavers.

G-Man
 
Upvote 0
g-man97 said:
TazGuy, here's my code. It is in a userform module.

Private Sub UserForm_Initialize()
Dim rng As Range
Dim LastRow As Long
Dim I As Long
Sheets("ScenariosTbl").Activate
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
For I = 5 To LastRow - 4
Set rng = ActiveSheet.Range("F" & I)
MsgBox "usrfrm initialize, listvalue is " & rng
If rng.Offset(0, 1) = "Active" Then
ScenariosMenu.ComboBox1.AddItem rng.Value
End If
Next I
End Sub

The msgbox (just for testing) shows the values but none of them show up in the combobox on the userform. Also, I need to use every other row, i.e. f5, f7, f9, etc. As it is now, this goes through every row and there is only data in every other, so I'd prefer not to have a bunch of blanks in my list.
You could change this line:
Code:
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
to this:
Code:
LastRow = Range("F65536").End(xlUp).Row
g-man97 said:
Is this code in the right module? I am launching the userform from a worksheet module with load FormName followed by FormName.Show. Are these in the right module?
Looks like everything's in the right place. You might want to just do a ScenariosMenu.Show in the Workbook_Open event, so that the form will pop up when the workbook opens. There's no need to load the form in that case, when the code gets to the .Show method, it will load it by itself.
g-man97 said:
Finally, how do I capture the value of the comobox? Would OffName = Me.ComboBox1.Caption do the trick?

While I'm at it, what goes in the ComboBox1_Change sub?
If you're in the ComboBox1_Change event, then ComboBox1.Value will reflect the currently selected value. If you're not in the userform's code, you'd have to specify that it's on a userform with ScenariosMenu.ComboBox1.Value

g-man97 said:
Sorry to be so dense. Thanks again everyone!! You're lifesavers.
G-Man

You're not dense. You have questions, you ask, someone answers, you learn, and then answer other people's questions. That's how it works. (y)
 
Upvote 0
TazGuy37,

Thank you for your quick reply and kind words. I eventually figured out, (through NUMEROUS trials and errors) that my problem was in this code:

If rng.Offset(0, 1) = "Active" Then
ScenariosMenu.ComboBox1.AddItem rng.Value
End If

I took out the if statement and the code works beautifully!



G-Man
 
Upvote 0

Forum statistics

Threads
1,206,755
Messages
6,074,756
Members
446,083
Latest member
kfunt

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