如何使用VBA将标题添加到Excel用户窗体中的多列列表框中
问题描述:
可以在多列列表框中设置标题,而不使用工作表范围作为源?
Is it possible to set up the headers in a multicolumn listbox without using a worksheet range as the source?
以下使用一组变量,分配给列表框的列表属性,标题显示为空白。
The following uses an array of variants which is assigned to the list property of the listbox, the headers appear blank.
Sub testMultiColumnLb()
ReDim arr(1 To 3, 1 To 2)
arr(1, 1) = "1"
arr(1, 2) = "One"
arr(2, 1) = "2"
arr(2, 2) = "Two"
arr(3, 1) = "3"
arr(3, 2) = "Three"
With ufTestUserForm.lbTest
.Clear
.ColumnCount = 2
.List = arr
End With
ufTestUserForm.Show 1
End Sub
答
我在列表框上方创建标签作为标题。您可能会认为,每当您的lisbox发生变化时,更改标签是一种皇室痛苦。你是对的 - 这是一个痛苦。第一次设置是一个痛苦,更少的变化。但我没有找到更好的方法。
No. I create labels above the listbox to serve as headers. You might think that it's a royal pain to change labels every time your lisbox changes. You'd be right - it is a pain. It's a pain to set up the first time, much less changes. But I haven't found a better way.