我正在使用VBA表单在两个列表框中显示一组范围值。在listbox1
标题中,然后使用以下代码在listbox2
中显示相关数据的每个输入:
'header
Set rng = sht.Range("A1:I1")
With ListBox1
.ColumnCount = 9
.ColumnWidths = "50;40;30;120;160;120;50;30;30"
.RowSource = rng.Address
End With
'dynamic data
Set rng1 = sht.Range(Cells(st, ukey), Cells(st + en - 1, utim))
With ListBox2
.ColumnCount = 9
.ColumnWidths = "50;40;30;120;160;120;50;30;30"
.RowSource = rng1.Address
End With
如何在标题后面的一个列表框中添加标题和数据?
另外,如何在另一组范围的列表框中添加第10列到第15列?
答案 0 :(得分:1)
如何在标题后的一个列表框中添加标题和数据?
联合两个范围,表示标题和像这样的值
Set rng = Union(rng, rng1)
.RowSource = rng.Address
这是一个有效的小样本,将两个范围合并为一个列表框:
Private Sub UserForm_Activate()
Dim rng As Range
Dim rngTitle As Range
Set rng = Range("D2:L3")
Set rngTitle = Range("D1:L1")
Set rng = Union(rng, rngTitle)
With ListBox1
.ColumnCount = 9
.ColumnWidths = "50;40;30;120;160;120;50;30;30"
.RowSource = rng.Address
End With
End Sub
这就是我得到的:
答案 1 :(得分:0)
我通过Vityata如上所述使用UNION合并了范围,并将它们移动到临时工作簿并使用以下代码显示:)
Set rngt1 = sht.Range("A1:I1")
Set rngt2 = sht.Range("X1:AE1")
Set rngt3 = sht.Range("AI1")
Set rngd1 = sht.Range(Cells(st, ukey), Cells(st + en - 1, utim))
Set rngd2 = sht.Range(Cells(st, ccat), Cells(st + en - 1, etag))
Set rngd3 = sht.Range(Cells(st, comt), Cells(st + en - 1, comt))
Set rngt = Union(rngt2, rngt1)
Set rngt = Union(rngt3, rngt)
Set rngd = Union(rngd2, rngd1)
Set rngd = Union(rngd3, rngd)
Workbooks.Add
rngt.Copy ActiveSheet.Range("A1")
rngd.Copy ActiveSheet.Range("A2")
Set rng = ActiveSheet.Range(Cells(2, 1), Cells(en + 1, 18))
With ListBox2
.ColumnCount = 18
.RowSource = rng.Address
.ColumnHeads = True
End With
谢谢Vityata的帮助:)