Get Excel sheet name and use as variable in macro

后端 未结 1 1504
走了就别回头了
走了就别回头了 2021-02-05 12:03

I\'m trying to find a way to use an Excel sheetname as a variable in a macro that I\'ve written. Every month I deal with a workbook that is sent to me with 2 sheets. Part of the

1条回答
  •  孤城傲影
    2021-02-05 12:30

    in a Visual Basic Macro you would use

    pName = ActiveWorkbook.Path      ' the path of the currently active file
    wbName = ActiveWorkbook.Name     ' the file name of the currently active file
    shtName = ActiveSheet.Name       ' the name of the currently selected worksheet
    

    The first sheet in a workbook can be referenced by

    ActiveWorkbook.Worksheets(1)
    

    so after deleting the [Report] tab you would use

    ActiveWorkbook.Worksheets("Report").Delete
    shtName = ActiveWorkbook.Worksheets(1).Name
    

    to "work on that sheet later on" you can create a range object like

    Dim MySheet as Range
    MySheet = ActiveWorkbook.Worksheets(shtName).[A1]
    

    and continue working on MySheet(rowNum, colNum) etc. ...

    shortcut creation of a range object without defining shtName:

    Dim MySheet as Range
    MySheet = ActiveWorkbook.Worksheets(1).[A1]
    

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