Solved

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

Posted on 2010-08-13
6
9,546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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