无法在Listbox.RowSource方法或Listbox.List方法中设置范围

时间:2018-11-21 14:45:38

标签: excel vba excel-vba listbox

行到达lbxFoilInfoDisplay.RowSource = "tblFoilInfoHelper!A1:" & Col_Letter(TotalColumnsCount("Foil Purchases.xlsm", "List_Box")) & TotalRowsCount("Foil Purchases.xlsm", "List_Box", "tblFoilInfoHelper")时出现错误:

enter image description here

当我尝试将其替换为: lbxFoilInfoDisplay.List = ThisWorkbook.Worksheets("List_Box").Range("A1:" & Col_Letter(TotalColumnsCount("Foil Purchases.xlsm", "List_Box")) & TotalRowsCount("Foil Purchases.xlsm", "List_Box", "tblFoilInfoHelper")),出现错误:

enter image description here

我相信表格名称正确无误,如下图所示:

enter image description here

Option Explicit
Dim filterRangeFoilProfile As Range, filteredRangeFoilProfile As Range
Private Sub cbxSupplier_AfterUpdate()
Dim Supplier_col As Long

lbxFoilInfoDisplay.RowSource = vbNullString
Supplier_col = ThisWorkbook.Worksheets("Foil Profile").ListObjects("tblFoilProfile").ListColumns("SUPPLIER").Index
filterRangeFoilProfile.AutoFilter Field:=Supplier_col, Criteria1:=cbxSupplier.Text
On Error Resume Next
Set filteredRangeFoilProfile = Intersect(filterRangeFoilProfile, filterRangeFoilProfile.Offset(1, 0)).SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Not filteredRangeFoilProfile Is Nothing Then
    ThisWorkbook.Worksheets("Foil Profile").ListObjects("tblFoilProfile").DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    ThisWorkbook.Worksheets("List_Box").Cells(2, 1).PasteSpecial
    lbxFoilInfoDisplay.RowSource = "tblFoilInfoHelper!A1:" & Col_Letter(TotalColumnsCount("Foil Purchases.xlsm", "List_Box")) & TotalRowsCount("Foil Purchases.xlsm", "List_Box", "tblFoilInfoHelper")
    'lbxFoilInfoDisplay.List = ThisWorkbook.Worksheets("List_Box").Range("A1:" & Col_Letter(TotalColumnsCount("Foil Purchases.xlsm", "List_Box")) & TotalRowsCount("Foil Purchases.xlsm", "List_Box", "tblFoilInfoHelper"))
End If

End Sub

用户定义的函数(用于添加上下文):

1。)

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

2。)

Function TotalRowsCount(WBString As String, WorksheetNameString As String, TableNameString As String) As Long

Dim Total_columns As Long
Dim i As Long

Total_columns = Workbooks(WBString).Worksheets(WorksheetNameString).ListObjects(TableNameString).ListColumns.Count

ReDim Rows_count(1 To Total_columns) As Long
For i = 1 To Total_columns
    With Workbooks(WBString).Worksheets(WorksheetNameString).ListObjects(TableNameString).ListColumns(i).Range
    Rows_count(i) = .Find(What:="*", _
        After:=.Cells(1), _
        Lookat:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False).Row
    End With
Next i
TotalRowsCount = WorksheetFunction.Max(Rows_count)

End Function

3。)

Function TotalColumnsCount(WBString As String, WorksheetNameString As String) As Long

Dim lastColumn As Long
lastColumn = Workbooks(WBString).Worksheets(WorksheetNameString).Cells(1, Columns.Count).End(xlToLeft).Column
TotalColumnsCount = lastColumn

End Function

1 个答案:

答案 0 :(得分:1)

使用工作表名称而不是表名称。即:“ List_Box!A1:”&...

lbxFoilInfoDisplay.RowSource = "List_Box!A1:" & Col_Letter(TotalColumnsCount("Foil Purchases.xlsm", "List_Box")) & TotalRowsCount("Foil Purchases.xlsm", "List_Box", "tblFoilInfoHelper")

可能会为此找到一些用途...还是没有。您也可以使用该表来执行此操作,但是仍然需要使用Sheetname才能获取单元格的地址。即。

ActiveSheet.ListObjects(1).Range.Cells(3, 1).Address

如果您需要引用表中的特定单元格,而不必关心表的位置,这将很有用。但这只是为了帮助您构建地址部分-而不是将表名替换为表名。