+ Reply to Thread
Results 1 to 3 of 3

VBA ComboBox Value Dependent on Conditional Value from Another ComboBox

  1. #1
    Registered User
    Join Date
    04-27-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    33

    VBA ComboBox Value Dependent on Conditional Value from Another ComboBox

    I am trying to populate one ComboBox with an array of values from within a worksheet, dependent on the value of another ComboBox on the same Userform. This functions, just not properly. If you change the ComboBox1 more than once, the list within ComboBox2 continues to add on previous series of values. If you are to try it yourself, open up an Excel workbook and in the first three columns enter in numbers in the first column, letters in the second and a combination in the third. I know I am over looking something.


    Please Login or Register  to view this content.
    Moderator's Note: Put code tags around codes. Highlight the codes then hit the "#" symbol. Thanks.
    Last edited by vlady; 07-04-2013 at 02:04 AM. Reason: code tags

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA ComboBox Value Dependent on Conditional Value from Another ComboBox

    You're not clearing the old column values from Combobox2 before adding the new column values.
    ComboBox2.Clear

    Try this...
    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 07-04-2013 at 02:16 AM.

  3. #3
    Registered User
    Join Date
    04-27-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: VBA ComboBox Value Dependent on Conditional Value from Another ComboBox

    I never thought I'd say this, but, AlphaFrog, you were right. I did you use a part of your strategy, the ComboBox2.Clear. If you don't mind me asking, how did you come up with something so intricate?

    Private Sub ComboBox1_Change()

    Dim x As Integer

    If ComboBox1.Value = "A" Then
    x = 1
    ComboBox2.Clear
    ElseIf ComboBox1.Value = "B" Then
    x = 2
    ComboBox2.Clear
    ElseIf ComboBox1.Value = "C" Then
    x = 3
    ComboBox2.Clear
    End If

    For i = 2 To Sheet1.Cells(1, x).SpecialCells(xlLastCell).Row
    If Sheet1.Cells(i, x) <> "" Then
    UserForm1.ComboBox2.AddItem (Sheet1.Cells(i, x))
    End If
    Next

    End Sub
    Private Sub userform_initialize()
    With ComboBox1
    .AddItem "A"
    .AddItem "B"
    .AddItem "C"
    End With
    End Sub


    Just for the record, now both methods work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1