首页 > 解决方案 > 带有 VBA 的 Excel:引用工作表代码名称而不是选项卡名称

问题描述

因为我每周都会更新选项卡名称,而不是选项卡名称,所以我想使用 VBA 项目表名称(Sheet17),如果我更改选项卡名称,它不会更改。

我的代码是:

With ThisWorkbook.Sheets("update raw data")
    Dim LastRow As Long
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

    Dim i As Integer

    For i = 4 To LastRow
 Cells(i, 56).Value = WorksheetFunction.Index(Sheet17.Range("O3:O300"),WorksheetFunction.Match(Cells(i,69).Value, Sheet17.Range("AB3:AB300"), 0))

     Next i

 End With

但它给了我这个错误:

'1004' -Unable to get the Match property of the WorksheetFunction class

标签: excelvbaworksheet

解决方案


尝试这个..

Sub test()
    Dim LastRow As Long, i As Integer
    With ThisWorkbook.Sheets("update raw data")
        LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row
        For i = 4 To LastRow
            .Cells(i, 56) = WorksheetFunction.Index(Sheet17.[O3:O300], WorksheetFunction.Match(.Cells(i, 69), Sheet17.[AB3:AB300], 0))
        Next i
    End With
End Sub

推荐阅读