Click to See Complete Forum and Search --> : ComboBox in VBA Excel


YamMilkTea
June 6th, 2009, 06:24 AM
Hi, i am new to VBA Excel. I need help with the code for combobox.
I have a list of data in excel sheet,with the data in the cells as

A1: Name B1: Sex
A2: Alice B2: F
A3: Alan B3: M
A4: Ben B4: M
A5: Jane B5: F
A6: Tony B6: M

I insert a combobox to the Userform, with 3 selection, 'All', 'F' and 'M'.
If i select 'F', i want to get

A1: Name B1: Sex
A2: Alice B2: F
A3: Jane B3: F

If i select 'M', i want to get
A1: Name B1: Sex
A2: Alan B2: M
A3: Ben B3: M
A4: Tony B4: M

If i select 'All', all the data remains the same.

How can i achieve that?

dglienna
June 6th, 2009, 01:10 PM
Either create two lists, or else apply a FILTER to the list, except that it looks like you've combined the fields anyways.

First option looks better

YamMilkTea
June 6th, 2009, 02:05 PM
Oh...what to u mean by creating 2 list? I need the code for the combobox...

dglienna
June 6th, 2009, 02:24 PM
An array list. You need to download the link in my signature of 101 Samples for VS2008. http://code.msdn.microsoft.com/vbsamples/

Here's what I meant

Dim ListOfMale as List(of String)
ListOfMale.AddItem "David M"

EDIT: If you're using VBA not Visual Studio, then let us know...

YamMilkTea
June 7th, 2009, 05:00 AM
I'm using VBA tks... =x

dglienna
June 7th, 2009, 12:44 PM
so, where is the data coming from? I don't have your worksheet

YamMilkTea
June 7th, 2009, 12:53 PM
I've solved using the filter method.

Private Sub UserForm_Initialize()
ComboBox1.AddItem "All"
ComboBox1.AddItem "F"
ComboBox1.AddItem "M"

Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "All"
Range("A1").AutoFilter
Case "F"
Range("A1").AutoFilter Field:=2, Criteria1:="F"
Case "M"
Range("A1").AutoFilter Field:=2, Criteria1:="M"
End Select
End Sub

Thanks. ^^