How can I get worksheet code name to activate a specific worksheet?

后端 未结 1 498
悲&欢浪女
悲&欢浪女 2020-12-04 04:20

I have a worksheet with the \"tab\" name of \"Rpt_Group\". I also renamed its code name to shData. When I use VBA to activate the worksheet using \"Rpt_Group\" it runs fine

1条回答
  •  粉色の甜心
    2020-12-04 04:41

    Here's one solution:

    Sub tester()
        Dim WBA As Workbook
    
        Set WBA = Workbooks("Book1")
        WorksheetByCodeName(WBA, "codeNameHere").Activate
    
    End Sub
    
    'Get a worksheet with matching codeName (or Nothing if no match)
    '    from a workbook wb
    Function WorksheetByCodeName(wb As Workbook, codeName As String)
        Dim ws As Worksheet, rv As Worksheet
        For Each ws In wb.Worksheets
            If ws.codeName = codeName Then
                Set rv = ws
                Exit For
            End If
        Next ws
        Set WorksheetByCodeName = rv
    End Function
    

    Probably want to check the return value before trying to do anything with it.

    0 讨论(0)
提交回复
热议问题