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

后端 未结 2 1677
野的像风
野的像风 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:08

    This works:

    Function SheetName as String
        SheetName = Application.Caller.Worksheet.Name
    End Function
    

    I should mention that as a practical matter, you should be very cautious about using caller-sensitive features like this in any function. Later on when you (or worse, someone else) are trying to debug something, it can be a real nightmare if you don't realize that that the function acts differently when called from code or in the debugger/immediate window, than it does in actual use.

提交回复
热议问题