Fill the combobox with the values of two different columns

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome. I am trying to fill a combobox with the values of two different columns. The following code does this, but I am having difficulty modifying it.
I want to fetch all column values up to the last row without selecting the last cell within the code, while ignoring the Empty cells if they are present in any column.

VBA Code:
Private Sub UserForm_Initialize()
  Set f = Sheets("Sheet1")
  'Lastrow = f.Cells(f.Rows.Count, 1).End(xlUp).Row
  a = Application.Transpose(f.[a1:a10])
  b = Application.Transpose(f.[D1:D10])
  c = Split(Join(a, ",") & "," & Join(b, ","), ",")
  Me.ComboBox1.List = c
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
change
Code:
  a = Application.Transpose(f.[a1:a10])
  b = Application.Transpose(f.[D1:D10])
to
Code:
  a = Filter(f.[transpose(if(a1:a30000<>"",a1:a30000))], False, 0)
  b = Filter(f.[transpose(if(d1:d30000<>"",d1:d30000))], False, 0)
 
Upvote 0
Solution
change
Code:
  a = Application.Transpose(f.[a1:a10])
  b = Application.Transpose(f.[D1:D10])
to
Code:
  a = Filter(f.[transpose(if(a1:a30000<>"",a1:a30000))], False, 0)
  b = Filter(f.[transpose(if(d1:d30000<>"",d1:d30000))], False, 0)
Very cool, this is what I was looking for. Thank you very much. I appreciate your help
 
Upvote 0
Try:
VBA Code:
Private Sub UserForm_Initialize()
    Dim Lastrow As Long
    Dim a As Variant, b As Variant, c As Variant
    Dim oDic As Object
    Dim i As Long

    Set oDic = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        a = Application.Transpose(.Range("A1:A" & Lastrow).Value)

        Lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row
        b = Application.Transpose(.Range("D1:D" & Lastrow).Value)
    End With

    c = Split(Join(a, ",") & "," & Join(b, ","), ",")

    On Error Resume Next
    For i = 0 To UBound(c)
        If Len(c(i)) > 0 Then
            oDic.Add i, c(i)
        End If
    Next i

    c = oDic.items()

    Me.ComboBox1.List = c
End Sub
Artik
 
Upvote 0
Try:
VBA Code:
Private Sub UserForm_Initialize()
    Dim Lastrow As Long
    Dim a As Variant, b As Variant, c As Variant
    Dim oDic As Object
    Dim i As Long

    Set oDic = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        a = Application.Transpose(.Range("A1:A" & Lastrow).Value)

        Lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row
        b = Application.Transpose(.Range("D1:D" & Lastrow).Value)
    End With

    c = Split(Join(a, ",") & "," & Join(b, ","), ",")

    On Error Resume Next
    For i = 0 To UBound(c)
        If Len(c(i)) > 0 Then
            oDic.Add i, c(i)
        End If
    Next i

    c = oDic.items()

    Me.ComboBox1.List = c
End Sub
Artik
Thanks, I tried your code. It works very well and fulfills exactly what is required. But I think I will use the first code because it is somewhat short, especially since I need to add 6 columns or more. Thank you again.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top