How can I determine the sheet name in a workbook that my function is sitting?

后端 未结 2 1686
野的像风
野的像风 2020-12-21 17:21

Imagine I have a xls workbook containing 3 tabs, SheetA, SheetB, and SheetC. I want to write a simple VBA function that returns the name of the sheet where the function is c

2条回答
  •  忘掉有多难
    2020-12-21 18:00

    You could do something like this:

    Function sheetName(rng As Range) As String
        sheetName = rng.Parent.Name
    End Function
    

    Just pass the range the sheet is calling from (even the same range the formula is in).

    In sheet1,

    =sheetName(A1)
    

    returns Sheet1

    In Sheet2

    =sheetName(Sheet1!A1)
    

    Returns Sheet1 as well.

提交回复
热议问题