How to add a header to multi-column list box in VBA userform

JJGey
Lounger
Posts: 49
Joined: 21 Dec 2015, 01:19

How to add a header to multi-column list box in VBA userform

Post by JJGey »

Is it possible to add a header to each column to this list box in a userform

Here is a sample code for the list box
This code is running outside any of the office applications like Word or Excel

Code: Select all

Private Sub UserForm_Initialize()

'Add multiple Columns to a listbox
ListBox1.Clear 'Make sure the Listbox is empty
ListBox1.ColumnCount = 3 'Set the column Amount
'Fill the Listbox
ListBox1.AddItem "Row Number 1" 'Additem creates a new row
ListBox1.AddItem "Row Number 2"
ListBox1.AddItem "Row Number 3"
ListBox1.List(0, 1) = "Column 2 Row 1" 'List(x,y) X is the row number, Y the column number
ListBox1.List(0, 2) = "Column 3 Row 1"
ListBox1.List(2, 1) = "Column 2 Row 3"
ListBox1.List(2, 2) = "Column 3 Row 3"
ListBox1.List(1, 1) = "Column 2 Row 2"
ListBox1.List(1, 2) = "Column 3 Row 2"

End Sub
Here is the result of the above code
2017-04-20_7-07-00.png
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 73871
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to add a header to multi-column list box in VBA user

Post by HansV »

Headers in a list box only work in Excel, and if you set the RowSource of the list box to a range of cells. Headers don't work in combination with AddItem.

You could fill range A1:C1 with headers, and A2:C5 with data, then use

ListBox1.RowSource = "Sheet1!A2:C5"

The header row is NOT included in the RowSource, but it should be located directly above the RowSource.
Regards,
Hans

JJGey
Lounger
Posts: 49
Joined: 21 Dec 2015, 01:19

Re: How to add a header to multi-column list box in VBA user

Post by JJGey »

This Code is running outside Excel and I search for solution and couldn't find

User avatar
HansV
Administrator
Posts: 73871
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to add a header to multi-column list box in VBA user

Post by HansV »

You cannot use headers in a userform list box outside Excel.
Regards,
Hans

JJGey
Lounger
Posts: 49
Joined: 21 Dec 2015, 01:19

Re: How to add a header to multi-column list box in VBA user

Post by JJGey »

Hans,
Thanks !

Rama2021
NewLounger
Posts: 1
Joined: 15 Dec 2021, 07:23

Re: How to add a header to multi-column list box in VBA userform

Post by Rama2021 »

hello
no need to use columns header
instead of that you can add label and write the text header of the column on the top of each column above of listbox on userform
so you will not need to mention the column header to each column

SpeakEasy
2StarLounger
Posts: 175
Joined: 27 Jun 2021, 10:46

Re: How to add a header to multi-column list box in VBA userform

Post by SpeakEasy »

You could always use a ListView instead