PDA

View Full Version : Solved: Making a Conditional Combobox from Excel data



Gavint
09-28-2006, 12:41 AM
I've setup a combobox on a Userform to extract data from an Excel worksheet using the below code

Private Sub UserForm_Initialize()
'define name codes
Dim ObjExcel As New Excel.Application
Dim wb As Excel.Workbook
Dim FName As Variant
Dim x As Long
Dim LastRow As Long

FName = "M:\TEMPLATES\NAME_CODES.xls"

Set wb = ObjExcel.Workbooks.Open(FName)

With wb.Sheets(1)
LastRow = .Range("C60").End(xlUp).Row
For x = 1 To LastRow
frmGetName.cbxType.AddItem (.Range("C" & x).Text)
Next x
End With

frmGetName.cbxType.SetFocus
End Sub

All works fine and the combobox is populated OK with the data from Excel.

What is the best way to make sure the user selection is from one of the loaded values (ie not just free text)?

fumei
09-28-2006, 09:47 AM
ComboBox1.MatchRequired = Truerequires the value of the control match an item in the list. If the user types in text that does not match an item, there is an error message. "Invalid property".

BTW: you need to use the instruction when you Initialize the userform.

mdmackillop
09-28-2006, 10:58 AM
BTW: you need to use the instruction when you Initialize the userform.

...or you can set it in the properties window for the combobox

Gavint
09-28-2006, 10:09 PM
Thanks!

Once again your help has saved me :hi: