Consulting

Results 1 to 7 of 7

Thread: Solved: VBA UserForm/ComboBox

  1. #1

    Solved: VBA UserForm/ComboBox

    Hello,

    I have a VBA programme that uses UserForm and ComboBox.

    The UserForm also has "OK", "Cancel" and "Clear" buttons.

    The ComboBox is populated with several options;

    With AnimalComboBox
    .AddItem "Dog"
    .AddItem "Cat"
    .AddItem "Horse"
    End With

    When run, the userform appears but the ComboBox dropdown is not populated, only when you press "Clear" do the options become available.

    Any help greatly appreciated.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    try adding this right after Horse
    [VBA]
    .ListIndex = 0
    [/VBA]

  3. #3
    Added that but it doesn't resolve the issue. All that seems to do is once I click "Clear", the dropdown box has "Dog" instead of it being blank.

    Thanks anyway

  4. #4
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    well, maybe you should post your full code.....

  5. #5
    As requested;

    [vba]Private Sub CancelButton_Click()
    Unload Me
    End Sub

    Private Sub ClearButton_Click()
    Call AnimalUserForm_Initialize
    End Sub

    Private Sub AnimalUserForm_Initialize()

    'Empty AnimalNameTextBox
    AnimalNameTextBox.Value = ""

    'Empty OwnerTextBox
    OwnerTextBox.Value = ""

    'Empty FoodCodeTextBox
    FoodCodeTextBox.Value = ""

    'Empty AnimalCategoryComboBox
    AnimalCategoryComboBox.Clear

    'Fill AnimalCategoryComboBox
    With AnimalCategoryComboBox
    .AddItem "Dog"
    .AddItem "Cat"
    .AddItem "Horse"
    End With

    'Set Focus on AnimalNameTextBox
    AnimalNameTextBox.SetFocus
    End Sub

    Private Sub OKButton_Click()

    Dim emptyRow As Long

    'Make Sheet1 Active
    Sheets(1).Activate

    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    'Export Data to worksheet
    Cells(emptyRow, 1).Value = AnimalNameTextBox.Value
    Cells(emptyRow, 2).Value = OwnerTextBox.Value
    Cells(emptyRow, 3).Value = FoodCodeTextBox.Value
    Cells(emptyRow, 4).Value = AnimalCategoryComboBox.Value

    End Sub
    [/vba]

  6. #6
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Well, what is happening is that you "don't" have an Initialize procedure, well at least you changed it. Change your AnimalUserForm_Initialize to UserForm_Initialize and you will find that things will work better.

  7. #7
    Thank you very much, that sorted it! You sir are a genius

Posting Permissions

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