excel 如何在vba中填充列表框?

slsn1g29  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(234)

我编写此代码是为了将工作表中的信息放入用户窗体中的列表框。这是我编写的代码,但它告诉我有些内容超出了范围,尽管我无法确定哪些内容超出了范围。

Private Sub PopulateSearchBox()

Dim wsTL2 As Worksheet
Set wsTL2 = Worksheets("Task List2")
Dim last_row As Long
last_row = wsTL2.Cells(wsTL2.Rows.Count, "C").End(xlUp).Row 'the last populated row in C so it covers the whole range of data that I need.

With Me.searchBox
    .ColumnHeads = True
    .ColumnCount = 3
    .ColumnWidths = "100,100,100"
    .RowSource = "Task List2!A1:C" & last_row
End With

End Sub
bq3bfh9z

bq3bfh9z1#

ComboBoxRowSource属性需要地址字符串。因此,您可以执行以下操作:

RowSource = wsTL2.Name & "!" & rng.Address
With Me.searchBox
    .ColumnHeads = True
    .ColumnCount = 3
    .ColumnWidths = "100,100,100"
    .RowSource = RowSource
End With

虽然很可能你会更喜欢.List属性?

Set rng = wsTL2.Cells(wsTL2.Rows.Count, "C").End(xlUp)
With Me.searchBox
    .ColumnHeads = True
    .ColumnCount = 3
    .ColumnWidths = "100,100,100"
    '.RowSource = RowSource
    .List = rng.Value2
End With

相关问题