当 ListBox.RowSource returns 没有结果时允许列表框不显示任何结果
Allow listbox to display no results when ListBox.RowSource returns no results
我目前正在使用以下代码在表单中填充列表框:
'Populate In-Form Table
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End With
但是,我还主动过滤列表框中显示的内容以及表单中的其他字段。这工作正常,除非我过滤掉所有结果。而不是收到这样的错误:"No cells were found." 我宁愿将表单中的 table 留空。
任何帮助将不胜感激,我已经为此苦苦思索了一段时间。
谢谢!
你可以试试这样的...
Dim n As Long
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
On Error Resume Next
n = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Rows.Count
On Error GoTo 0
If n > 0 Then
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End If
End With
如果您要过滤 Excel Table,您可以按以下方式计算过滤的行...
Dim n As Long
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
On Error Resume Next
n = ActiveSheet.ListObjects("MasterDataTable").Range.Resize(, 1).SpecialCells(xlCellTypeVisible).Count
On Error GoTo 0
If n > 0 Then
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End If
End With
End Sub
我目前正在使用以下代码在表单中填充列表框:
'Populate In-Form Table
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End With
但是,我还主动过滤列表框中显示的内容以及表单中的其他字段。这工作正常,除非我过滤掉所有结果。而不是收到这样的错误:"No cells were found." 我宁愿将表单中的 table 留空。
任何帮助将不胜感激,我已经为此苦苦思索了一段时间。
谢谢!
你可以试试这样的...
Dim n As Long
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
On Error Resume Next
n = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Rows.Count
On Error GoTo 0
If n > 0 Then
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End If
End With
如果您要过滤 Excel Table,您可以按以下方式计算过滤的行...
Dim n As Long
With ListBox_InFormTable
.ColumnCount = 4
.ColumnWidths = "100;100;100;50"
On Error Resume Next
n = ActiveSheet.ListObjects("MasterDataTable").Range.Resize(, 1).SpecialCells(xlCellTypeVisible).Count
On Error GoTo 0
If n > 0 Then
.RowSource = ws.Range("MasterDataTable").SpecialCells(xlCellTypeVisible).Address
End If
End With
End Sub