【发布时间】:2019-12-24 18:47:27
【问题描述】:
我有一个用户窗体列表框,我想在其中填充来自 excel 工作表的过滤范围(仅可见单元格)。这句话出现了问题:userForm.listBox.RowSource = dataRng.Address 其中 dataRng 是可见单元格的范围 (Set dataRng = sht.Range(Cells(startRow, 1), Cells(lastRow, 4)).SpecialCells(xlCellTypeVisible))。
我也尝试使用动态数组 (MyArray(i, j) = dataRng.Cells(i, j).Value) 获取可见范围,然后用它填充列表框,但没有成功(出现标题问题,但似乎是最有效和最快的解决方案)。
我在几年前找到了this unanswered question,但我正在寻找更好的解决方案。
Private Sub listBox_Change()
Dim startRow,lastRow As Integer
Dim sht As Worksheet
'Dim MyArray As Variant 'variant, receives one based 2-dim data field array
Set sht = Worksheets("SheetName")
Call filterData(sht) 'filter data in SheetName
startRow = sht.Cells(Rows.Count, 1).End(xlDown).Row 'get initial row of filtered range
lastRow = sht.Cells(Rows.Count, 1).End(xlUp).Row 'get last row of filtered range
Set dataRng = sht.Range(Cells(startRow, 1), Cells(lastRow, 4)).SpecialCells(xlCellTypeVisible) 'get range of visible cells in SheetName
With userForm.listBox
.ColumnCount = 4
.ColumnWidths = "90;90;0;90"
.RowSource = dataRng.Address
' For i = startRow To lastRow
' For j = 1 To 3
' MyArray(i, j) = dataRng.Cells(i, j).Value
' Next j
' Next i
End With
End Sub
运行时错误 380:无法设置 RowSource 属性。无效的属性值。
【问题讨论】:
标签: excel vba filter listbox populate