Solved

What is the VBA to set column head in list box?

Posted on 2010-08-13
6
6,197 Views
Last Modified: 2012-05-10
What is the VBA to set column head in list box?

I want it to read from an array.

Thanks
0
Comment
Question by:kosenrufu
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:2toria
ID: 33427696
It would seem you can only do this if you bind the listbox to a range.  Various discussions below:-

http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/72669064-af0b-484c-ba9a-eed2105d46d7
http://www.excelforum.com/excel-general/556357-add-listbox-header-using-vba.html

You're probably better off learning about the listview control - I tend to use this instead of listbox as it offers greater functionality and customisation.

Matt
0
 
LVL 45

Accepted Solution

by:
patrickab earned 500 total points
ID: 33427800
kosenrufu,

What you want is shown in the VBA code below and in the attached file.

Note that is you want the column header visible in a ListBox the obviously this is needed .ColumnHeads = True, however as soon as that is specified you then need to make sure that the data range does NOT include the column header. Thus the data range rng starts at row 2, not row 1.

BTW if you want to use a multi-column ListBox thereis no way that you can have column headers in Excel 2002 (and later I believe) - it's a simple omission by MS.

Hope that helps

Patrick
Private Sub UserForm_Initialize()
Dim rng As Range

With Sheets("Sheet1")
    Set rng = Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With

With UserForm1.ListBox1
    .ColumnHeads = True
    .RowSource = rng.Address
End With

End Sub

Open in new window

ListBox-ex-01.xls
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33429958
>>"BTW if you want to use a multi-column ListBox thereis no way that you can have column headers in Excel 2002 (and later I believe) - it's a simple omission by MS."
Really? It certainly works fine in 2003 and I can't recall it being an issue in 2002, though I never use the rowsource myself, so I can't be sure. I don't believe I've ever heard of that as a bug - do you have an MSKB or other source to hand?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 45

Expert Comment

by:patrickab
ID: 33430275
>do you have an MSKB or other source to hand?

Yep, really. It is mentioned by Walkenbach ISBN 0-7645-4799-2 page 432. I should have said that it only applies if the list source for a multi-column ListBox is a VBA array. So thanks for picking me up on this so that I can be more specific. Personally I have found that shortcoming irritating but as I like to assign VBA arrays to the list source I have to live with that issue.

Patrick
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33430325
Ah, yes - that's a different issue (and applies whether it's single or multi-column). If you use Rowsource you can have headers; if not, you can't.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 33465241
kosenrufu - Thanks for the grade - Patrick
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now