我如何确定我的功能所在的工作簿中的工作表名称? [英] How can I determine the sheet name in a workbook that my function is sitting?

查看:97
本文介绍了我如何确定我的功能所在的工作簿中的工作表名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个包含3个选项卡,SheetA,SheetB和SheetC的xls工作簿。我想编写一个简单的VBA函数,返回函数调用的表的名称。

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 called.

Function SheetName as String
    SheetName = ???
End Function

所以如果我在Tab SheetB中调用= SheetName(),它总是返回SheetB。

So if I call =SheetName() in Tab SheetB, it will ALWAYS return SheetB.

注意:

 ActiveSheet.Name

无效,因为如果您在SheetA上并计算工作簿,它将返回SheetA。

doesn't work because if you are on the SheetA and calculate the workbook, it will return SheetA.

推荐答案

这样做:

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.

这篇关于我如何确定我的功能所在的工作簿中的工作表名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆