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?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How exactly id AccidentsHeader defined?

If your data is in, say, A1:D5 with the header row in A1:D1 then to show the headers correctly in the listbox AccidentsHeader should refer to A2:D5.
 
Upvote 0
The row it populates is dynamic.

The header row is constant and I haven't yet defined AccidentsHeader.
 
Upvote 0
If you haven't defined AccidentsHeader yet then you should get an error when you try and use it for the row source property of the listbox.
 
Upvote 0
The headers for a listbox are taken from the row above the range you use as the row source.

For example if your data, including headers, was in A1:D5 then to have the values from A1:D1 appear as headers in the listbox you would set the RowSource to A2:D5.
 
Upvote 0
Only change one row down in your named range. For example, if your named range has "$A$1:$H$1200" then change it to "$A$2:$H$1200"
Try again
 
Upvote 0
Then I did not understand.
Can you explain what you need?

Do you want to fill the listbox by referring to the named range "AccidentsHeader"?
If so, try the following:

VBA Code:
Private Sub CommandButton1_Click()
  Dim r As Range, sh As Worksheet, lr As Long
  Set sh = Sheets(Range("AccidentsHeader").Parent.Name)
  Set r = sh.Range("AccidentsHeader")
  lr = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row
  ListBox1.RowSource = sh.Name & "!" & r.Offset(1).Resize(lr, r.Columns.Count).Address
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,700
Members
448,293
Latest member
jin kazuya

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