Consulting

Results 1 to 8 of 8

Thread: populate comboboxes multicolumns ignoring empty filds

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    populate comboboxes multicolumns ignoring empty filds

    Hello

    I would like your help. so, I have an data sheet with some tables.

    header 1|%|%| table1 with data
    data1, data2 etc
    header 2|%|%| table2 with data
    data1, data2 etc
    header 3|%|%| table2 with data
    data1, data2 etc

    then I have an combobox that is populated with the header 1, header 2, header3 ignoring the empty fields. I do not know how to make an multicolumns combobox for the second row and third row in the same combobox.

    attach it is the example. thanx for looking and for help

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What exactly do you want to see in the listbox?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    the next 2 rows (2, 3)

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    The header + number from row 2 + number from row 3

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Try this.
    [VBA] With ComboBox1
    For Each rngNext1 In myRange1
    If rngNext1 <> "" Then
    .AddItem rngNext1
    .Column(1, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 1).Value
    .Column(2, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 2).Value
    End If
    Next rngNext1
    End With
    [/VBA]

    David


  6. #6
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    thx, Tinbendr. It is working
    one extra thing:

    how can be tell to excel that to take the ranges from Sheet3 (where the data stays)
    [VBA]
    'Set wsSheet = ActiveSheet
    Set wsSheet = ActiveSheet.Worksheets("Sheet3")
    With wsSheet
    Set rngNext1 = Worksheets("Sheet3").Range("A5").End(xlUp).Offset(1, 0)
    End With
    rngNext1.Select
    Set myRange1 = Range("a59", rngNext1)

    'populate Group combobox list

    With ComboBox1
    For Each rngNext1 In myRange1
    If rngNext1 <> "" Then
    .AddItem rngNext1
    .Column(1, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 1).Value
    .Column(2, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 2).Value
    End If
    Next rngNext1
    End With
    [/VBA]

    like this, it is not working

  7. #7
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by white_flag
    like this, it is not working
    Please be specific in your reply. This doesn't tell me anything.

    I cleaned up this code quite a bit. I realize that you're just trying to get it to work, and cleanup may come later, but clutter can be a frustrating thorn-in-the-side.

    Quickly, a few comments.

    The use of Select is usually unnecessary. The macro recorder uses it exclusively, but it really in a inefficient way to deal with code.

    Using a single statement in a With nest is unnecessary. Just define it and go.

    You can reuse variables, but it's a bad practice. It'll usually come back to haunt you later. While the reuse of RngNext1 is possible, you should specifically reset it to Nothing before reassigning it to a different task. Better still is to use a different variable with a descriptive name so you can understand your code (especially in the future.)

    Finally, I'm still unsure about the Range of data. Is the data at A5:A59, or is there some dynamic element I'm unaware? If the range is fixed, then just define it as so and let the If/Then filter any empty cells.

    [vba]Private Sub UserForm_Initialize()

    Dim wsSheet As Worksheet
    Dim LastCell As Range
    Dim rngNext1 As Range
    Dim myRange1 As Range

    Set LastCell = Worksheets("Sheet3").Range("A5").End(xlUp)
    Set myRange1 = Range("a59", LastCell)

    'populate Group combobox list
    With ComboBox1
    For Each rngNext1 In myRange1
    If rngNext1 <> "" Then
    .AddItem rngNext1
    .Column(1, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 1).Value
    .Column(2, ComboBox1.ListCount - 1) = rngNext1.Offset(0, 2).Value
    End If
    Next
    End With

    End Sub
    [/vba]

    David


  8. #8
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    thx for pointing some issues.
    Subscript out of range..for me it is clear but for excel it is not (your code). The excel consider this, that it is not an valid range. the range data will be dynamic (for the moment I think it is better)

Posting Permissions

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