Fill a ListBox with values from different columns

brendalpzm

Board Regular
Joined
Oct 3, 2022
Messages
59
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Let's say I have the following table

Sheet name: Values

A
B
C
D
1FolioModelYearStatus
2156Forte2022Delivered
3452526Rio2023In repair
4785Stinger2023Delivered
5475Forte2020In progress

An I want to fill an ActiveX List Box but only with the values from columns A,B and D, and also only from year 2023.

How would the VBA code could work? my list box is in a form inside the excel that pops up when I click an activex button.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Let's say I have the following table

Sheet name: Values

A
B
C
D
1FolioModelYearStatus
2156Forte2022Delivered
3452526Rio2023In repair
4785Stinger2023Delivered
5475Forte2020In progress

An I want to fill an ActiveX List Box but only with the values from columns A,B and D, and also only from year 2023.

How would the VBA code could work? my list box is in a form inside the excel that pops up when I click an activex button.
Do you want this solution to work on all of these versions: 365, 2021, 2019, 2016?

How many rows have you got?
 
Upvote 0
Do you want this solution to work on all of these versions: 365, 2021, 2019, 2016?

How many rows have you got?
I'm currnetly using 365, but it will be nice just in case if it works in other versions as well.

The number of row can increase or decrease, so it might be dynamic range.
 
Upvote 0
I'm currnetly using 365, but it will be nice just in case if it works in other versions as well.

The number of row can increase or decrease, so it might be dynamic range.

Put this in the Userform Code Module.

Listbox is called lstValues.

VBA Code:
Private Sub UserForm_Initialize()
Dim arrData() As Variant
Dim lngIndex As Long
Dim lngRow As Long

    With Me.lstValues
        .ColumnCount = 3
        .ColumnWidths = "60,60,60"
        .Font.Size = 12
    End With
    
    arrData = Worksheets("Values").Range("A1").CurrentRegion.Value

    lngRow = 0
    
    For lngIndex = LBound(arrData, 1) + 1 To UBound(arrData, 1)
        If arrData(lngIndex, 3) = 2023 Then
            With Me.lstValues
                .AddItem
                .List(lngRow, 0) = arrData(lngIndex, 1)
                .List(lngRow, 1) = arrData(lngIndex, 2)
                .List(lngRow, 2) = arrData(lngIndex, 4)
            End With
            lngRow = lngRow + 1
        End If
    Next lngIndex

End Sub
 
Upvote 1
Solution
Put this in the Userform Code Module.

Listbox is called lstValues.

VBA Code:
Private Sub UserForm_Initialize()
Dim arrData() As Variant
Dim lngIndex As Long
Dim lngRow As Long

    With Me.lstValues
        .ColumnCount = 3
        .ColumnWidths = "60,60,60"
        .Font.Size = 12
    End With
   
    arrData = Worksheets("Values").Range("A1").CurrentRegion.Value

    lngRow = 0
   
    For lngIndex = LBound(arrData, 1) + 1 To UBound(arrData, 1)
        If arrData(lngIndex, 3) = 2023 Then
            With Me.lstValues
                .AddItem
                .List(lngRow, 0) = arrData(lngIndex, 1)
                .List(lngRow, 1) = arrData(lngIndex, 2)
                .List(lngRow, 2) = arrData(lngIndex, 4)
            End With
            lngRow = lngRow + 1
        End If
    Next lngIndex

End Sub
IT WORKED THANK YOU SO MUCH
 
Upvote 0
Put this in the Userform Code Module.

Listbox is called lstValues.

VBA Code:
Private Sub UserForm_Initialize()
Dim arrData() As Variant
Dim lngIndex As Long
Dim lngRow As Long

    With Me.lstValues
        .ColumnCount = 3
        .ColumnWidths = "60,60,60"
        .Font.Size = 12
    End With
   
    arrData = Worksheets("Values").Range("A1").CurrentRegion.Value

    lngRow = 0
   
    For lngIndex = LBound(arrData, 1) + 1 To UBound(arrData, 1)
        If arrData(lngIndex, 3) = 2023 Then
            With Me.lstValues
                .AddItem
                .List(lngRow, 0) = arrData(lngIndex, 1)
                .List(lngRow, 1) = arrData(lngIndex, 2)
                .List(lngRow, 2) = arrData(lngIndex, 4)
            End With
            lngRow = lngRow + 1
        End If
    Next lngIndex

End Sub
If I want to show the headers of those columns what shoul I modify from here?
 
Upvote 0
If I want to show the headers of those columns what shoul I modify from here?
See image.

Set up another listbox called lstHeaders and place it above the lstValues one.

This will just have one row that cannot be selected.

Replace the previous code with this.

VBA Code:
Private Sub UserForm_Initialize()
Dim arrData() As Variant
Dim lngIndex As Long
Dim lngRow As Long

    arrData = Worksheets("Values").Range("A1").CurrentRegion.Value

    With Me.lstHeaders
        .ColumnCount = 3
        .ColumnWidths = "60,60,60"
        .Font.Size = 12
        .Font.Bold = True
        .Enabled = False
         .AddItem
        .List(lngRow, 0) = arrData(1, 1)
        .List(lngRow, 1) = arrData(1, 2)
        .List(lngRow, 2) = arrData(1, 4)
    End With

    With Me.lstValues
        .ColumnCount = 3
        .ColumnWidths = "60,60,60"
        .Font.Size = 12
    End With
    
    lngRow = 0
    
    For lngIndex = LBound(arrData, 1) + 1 To UBound(arrData, 1)
        If arrData(lngIndex, 3) = 2023 Then
            With Me.lstValues
                .AddItem
                .List(lngRow, 0) = arrData(lngIndex, 1)
                .List(lngRow, 1) = arrData(lngIndex, 2)
                .List(lngRow, 2) = arrData(lngIndex, 4)
            End With
            lngRow = lngRow + 1
        End If
    Next lngIndex

End Sub
 

Attachments

  • headers.JPG
    headers.JPG
    63.6 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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