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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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