Problem setting combobox

G

Guest

I have a userform with multiple comboboxes (combobox1 -> combobox40)

I want to fill the combobox, preferably using .additem , so that the user
can't change it. Somehow, I can't set the cboBox variable.

This is what I've tried:

Sub FillAllComboBoxes()
dim cboBox as msforms.combobox
dim frmSettings as userform

frmSettings = SettingsForm ' the name of my userform

With frmSettings
.combobox1.additem = "FirstItem" 'works just fine
set cboBox = .combobox1 ' bugs out here
call FillcomboBox(cboBox)
End with
end sub


Sub FillcomboBox(cboBox as MsForms.combobox)

cboBox.additem = "value1"
cboBox.additem = "value2"

End sub

I can't get passed the
set cboBox = .combobox1
line without it bugging out.

Ideally, I'd like to cycle through the combobox using their numbers. I saw
this posting by Tom Ogilvy, but can't get it to work for a userform that is
not part of an active worksheet.

' this declaration is important
Dim cbx as MsForms.Combobox
.. . .
For i = 400 To myrow - 10 Step -1
With ActiveSheet.OLEObjects("ComboBox" & i)
.name = "ComboBox" & i + 1
set cbx = .Object
cbx.Name = "ComboBox" & i + 1
end with
Next i

--
 
S

Simon Lloyd

Hi try this modified code, it worked for me for Combobox1 i'm using
Excel 2003 Windows XP.

Regards,
Simon

Sub FillAllComboBoxes()
Dim cboBox As MsForms.ComboBox
With settingsform
..ComboBox1.AddItem "FirstItem"
Set cboBox = .ComboBox1
Call FillcomboBox(cboBox)
End With
End Sub


Sub FillcomboBox(cboBox As MsForms.ComboBox)

cboBox.AddItem "value1"
cboBox.AddItem "value2"

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top