Solved

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

Posted on 2010-08-13
6
8,323 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

839 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