VBA ListBox List - Want only selected columns to be listed

vmjamshad

New Member
Joined
Jan 5, 2017
Messages
16
Hi All,

ListBox1.ColumnCount = 4
ListBox1.ColumnWidths = "25;120;60;80"
Me.ListBox1.list = Sheets("Sheet1").Range("A2:AF100").Value

My data range is ("A2:BB100")
Here in this list box, I want only 2 columns (Column A and Column AF)
Can anybody help me to solve this,

1. I want to use variables for list range and data range. The ranges will vary.

Thanks - Jamshad
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
If you only want 2 columns in the listbox why are you setting ColumnCount to 4?
 
Upvote 0
If you only want 2 columns in the listbox why are you setting ColumnCount to 4?
Hi Norie, that is just as an example. I have around 300 columns and need to select only few columns -may be around 10. that also will not be in sequence. And the row length will keep on changing. So I will have to use a dynamic data range.
 
Upvote 0
Solved with the below code:

Code:
Private Sub UserForm_Initialize()
With ListBox1
    .ColumnCount = 4
    .ColumnWidths = "130;30;30;130"
End With


LstRow = Cells(Rows.Count, 1).End(xlUp).Row
For a = 0 To LstRow - 2
    b = a + 2
    ListBox1.AddItem
    ListBox1.list(a, 0) = Cells(b, 4)
    ListBox1.list(a, 1) = Cells(b, 1)
    ListBox1.list(a, 2) = Cells(b, 3)
    ListBox1.list(a, 3) = Cells(b, 2)


Next a


End Sub

Thanks - Jamshad
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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