Assuming that your country column is column A, place the following
code into the VBA section of the form containing your three ComboBoxes
(referred to as cmbBoxCountry, cmbBoxState and cmbBoxProduct) and give
it a try:

Private Sub cmbBoxCountry_Change()
    Call UpdateComboBox("B", cmbBoxState, cmbBoxCountry)
End Sub

Private Sub cmbBoxState_Change()
    Call UpdateComboBox("C", cmbBoxProduct, cmbBoxState)
End Sub

Private Sub UserForm_Activate()
    Call UpdateComboBox("A", cmbBoxCountry)
End Sub

Private Sub UpdateComboBox(sourceCol As String, ByRef c As
MSForms.ComboBox, Optional cRef As MSForms.ComboBox)
    c.Clear
    Dim r As Range
    Set r = Range(Range(sourceCol & 2), Range(sourceCol &
Rows.Count).End(xlUp))
    Dim rCell As Range
    If cRef Is Nothing Then
        For Each rCell In r.Cells
            If Not IsInComboBox(rCell.Value, c) Then c.AddItem
(rCell.Value)
        Next rCell
    Else
        For Each rCell In r.Cells
            If rCell.Offset(, -1).Value = cRef Then
                If Not IsInComboBox(rCell.Value, c) Then c.AddItem
(rCell.Value)
            End If
        Next rCell
    End If
    If c.ListCount > 0 Then c.ListIndex = 0
End Sub
Private Function IsInComboBox(s As String, c As MSForms.ComboBox) As
Boolean
    Dim i As Integer
    For i = 0 To c.ListCount - 1
        If c.List(i) = s Then
            IsInComboBox = True
            Exit Function
        End If
    Next i
    IsInComboBox = False
End Function

Hope this helped,
Rolf Jaeger
SoarentComputing
http://soarentcomputing.com/SoarentComputing/ExcelSolutions.htm


On Nov 8, 7:47 am, Meimei <xxu8810...@gmail.com> wrote:
> Hello,
>
> I have a question on using VBA to programme several ComboBoxes whose
> items are dynamically filled?
> I have a worksheet containing country names, states, and product, such
> as
>     Country        State         Product
> 1  US           MA              Banana
> 2  US           MA              Orange
> 3  US           WV              Apple
> 4  US           WV              Apple
> 5  US           CA              Banana
> 6  Canada               Quebec  Orange
> 7  Canada               Quebec  Orange
> 8  Canada               Albert  Apple
> 9  Canada               Albert  Banana
>
> I want to the 1st ComboBox to read the list of country names and
> filled by the unique country names, in this, I have two country names,
> US and Canada. After the 1st ComboBox is selected, I want the 2nd
> ComboBox to be dynamically filled with the appropriate state/province
> names, i.e., if the user chooses US, then the 2nd ComboBox only have
> the choices of MA, WV, and CA. Similarly for the 3rd ComboBox, if the
> user chooses US and then WV, the 3rd ComboBox should list Apple.
>
> So I figure that the code should dynamically search for unique country
> names, state names, and product names, and also be able to link the
> appropriate list of states to country, and appropriate list of product
> to state or provinces. But I don't have a clear idea how to do this.
> Can someone help me with some sample codes?
>
> Thanks a lot!
--~--~---------~--~----~------------~-------~--~----~
----------------------------------------------------------------------------------
Some important links for excel users:
1. Excel and VBA Tutorials(Video and Text), Free add-ins downloads at 
http://www.excelitems.com
2. Excel tutorials at http://www.excel-macros.blogspot.com
3. Learn VBA Macros at http://www.vbamacros.blogspot.com
4. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 

To post to this group, send email to excel-macros@googlegroups.com
If you find any spam message in the group, please send an email to:
Ayush Jain  @ jainayus...@gmail.com or
Ashish Jain @ 26may.1...@gmail.com
<><><><><><><><><><><><><><><><><><><><><><>
HELP US GROW !!

We reach over 6,500 subscribers worldwide and receive many nice notes about the 
learning and support from the group. Our goal is to have 10,000 subscribers by 
the end of 2009. Let friends and co-workers know they can subscribe to group at 
http://groups.google.com/group/excel-macros/subscribe
-~----------~----~----~----~------~----~------~--~---

Reply via email to