Solved

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

Posted on 2010-08-13
6
7,633 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

813 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