Listbox Header

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Have a named range called "AccidentsHeader"

Within my code I have:

Code:
Private Sub CommandButton1_Click()

ListBox1.RowSource = "AccidentsHeader"

....

End Sub

ColumHeaders set to TRUE

But I keep getting Column A, Column B, Column C... etc...

Any ideas?
 
I don't seem to see any code to populate the listbox in the sub CommandButton1_Click.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I am unsure which line of code to include for this now.

' sh.Range("A" & Rows.Count).End(xlUp)(2).Resize(1, UBound(ary) + 1).Value = ary
' lr = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row

Those lines of code populate the record again at the base of the data which is not what it is supposed to do.
 
Upvote 0
You would use the same code you used previously to populate the listbox.
VBA Code:
  ListBox1.RowSource  = r.Offset(1).Resize(lr, r.Columns.Count).Address(External:=True)
 
Upvote 0
That doesn't quite work, it throws up an error on that row.

I think it is because it is trying to find the value in row 2, which is now blank?
 
Upvote 0
1. Using Rowsource is unreliable especially if you plan to add or delete the list item in the lsitbox.

This is from VBA for smarties How to fill a Combobox / Listbox
It says:
"If you use 'rowsource' you make a direct link to a certain range in a worksheet.
Combined with the combobox it can cause Excel to blackout, because any change in the combobox will be transferred directly to the 'source'.
Excel has proven not to be able to do this correctly.
Besides: the changing of the source at every change in the combobox slows down your code.
You should reduce the reading/writing from/to a workbook in your code as much as possible."

Without Rowsource, you can insert the range directly into the listbox like this:
ListBox1.List = Sheets("Sheet1").Range("A1:D10").Value

2. As for the listbox header, you can create another listbox, place it above the first listbox, populate the table header into it. You can play with many properties to change its appearance like background color, font size etc. Like this:
2020-01-16_230229.jpg
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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