Dynamic columns on a userform listbox

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
I have a userform with a tabstrip 6 tabs and a listbox, depending on which tab you are will determine how many columns the listbox should have. This is from 2 columns to 18. I therefore have to set the code to show for each column type in the tab. I don't want to set it for the max number of columns 18 as it creates a horizontal scroll bar at the bottom and also flickers more when being populated.

Is there a way it could show the columns dynamically, depending on columns with data?

VBA Code:
    Userform1.ListBox1.ColumnCount = 2
    Userform1.ListBox1.ColumnWidths = "350;250"
    Userform1.ListBox1.RowSource = "'" & Sheet19.Name & "'!$A$1:$b$" & Sheet19.Cells(Sheet19.Rows.Count, 1).End(xlUp).Row

or

VBA Code:
    Userform1.ListBox1.ColumnCount = 6
    Userform1.ListBox1.ColumnWidths = "350;250;250;250;250;250"
    Userform1.ListBox1.RowSource = "'" & Sheet19.Name & "'!$A$1:$F$" & Sheet19.Cells(Sheet19.Rows.Count, 1).End(xlUp).Row

and so on
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Just set the columncount property to -1
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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