工作表名称可能会更改。因此,我想根据工作表的代号设置工作表对象。我怎样才能做到这一点?

到目前为止,我最好的尝试是:

Sub UpdateNameDropdown()

    Dim wksName As String
    wksName = ThisWorkbook.Sheets(Sheet16).Name

    Dim wks As Worksheet
    Set wks = Sheets(wksName)

End Sub


但是我在wksName = ThisWorkbook.Sheets.Sheet16.Name行上收到类型不匹配错误

最佳答案

这使用代号作为字符串

Sub CodeIt()
    Dim CodeName As String
    CodeName = "Sheet1"
    Dim WS As Worksheet, GetWorksheetFromCodeName As Worksheet
    For Each WS In ThisWorkbook.Worksheets
        If StrComp(WS.CodeName, CodeName, vbTextCompare) = 0 Then
            Set GetWorksheetFromCodeName = WS
            Exit For
        End If
    Next WS
    MsgBox GetWorksheetFromCodeName.Name
End Sub

关于excel - 如何基于工作表的代号设置工作表对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25808370/

10-15 02:24