?
Solved

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

Posted on 2010-08-13
6
Medium Priority
?
11,254 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 2000 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
Technology Partners: 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!

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

765 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