简体   繁体   English

如何参考Excel VBA中使用函数的工作簿?

[英]How to refer to the workbook where a function is used in Excel VBA?

I have created a user-defined function called SheetAtIndex that returns the name of the sheet at the specified index in a workbook. 我创建了一个名为SheetAtIndex的用户定义函数,它返回工作簿中指定索引处的工作表名称。

At first I used ActiveWorkbook.Sheets(Index).Name to get the name of the sheet. 起初我使用ActiveWorkbook.Sheets(Index).Name来获取工作表的名称。 Anytime I switch to another open workbook the results became messed up. 无论何时我切换到另一个打开的工作簿,结果都搞砸了。 No surprise there. 这并不奇怪。

ThisWorkbook.Sheets(Index).Name worked for some time till I realized I have to duplicate the code in every workbook that needs the UDF. ThisWorkbook.Sheets(Index).Name工作了一段时间,直到我意识到我必须在每个需要UDF的工作簿中复制代码。 In addition every file that needs the function must be saved as a macro-enable workbook 此外,每个需要该函数的文件都必须保存为启用宏的工作簿

Final decision : Put the function in my personal macro book. 最终决定 :将该功能放入我的个人宏书中。

Problem : How to reference the workbook/worksheet where the function will be called from? 问题 :如何引用将从中调用函数的工作簿/工作表?

A UDF can get a reference to the cell that called it with Application.ThisCell UDF可以获取对使用Application.ThisCell调用它的单元格的引用

Once you have a reference to the calling cell, you can get the worksheet ( .WorkSheet ) and workbook ( .WorkSheet.Parent ) from there 一旦你有一个对调用单元格的引用,你可以从那里得到工作表( .WorkSheet )和工作簿( .WorkSheet.Parent

Function SheetAtIndex(Index As Variant) As String
    SheetAtIndex = Application.ThisCell.Worksheet.Parent.Worksheets(Index).Name
End Function

Or, to get a reference to the Worksheet 或者,获取对工作表的引用

Function SheetAtIndex(Index As Variant) As Worksheet
    Set SheetAtIndex = Application.ThisCell.Worksheet.Parent.Worksheets(Index)
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA-如何在Excel VBA中引用工作簿内置的文档属性? - Excel vba - How to refer to workbook builtin document properties in excel VBA? 如何从 PowerPoint VBA 参考 Open Excel 工作簿? (没有 GetObject) - How to refer to an Open Excel Workbook from PowerPoint VBA ? (without GetObject) 如何通过另一个工作簿中的VBA对象名称引用Excel工作表? - How to refer to a Excel Worksheet by its VBA Object Name in another Workbook? 如何在VBA中引用打开的工作簿? - How to refer to an open workbook in VBA? Excel VBA:引用与活动工作簿不同的工作簿中的数组表 - Excel VBA: refer to an array table in a different workbook than the active workbook 我如何设计 VBA 中的代码,以便在 Excel 中引用以前新创建的工作簿? - How can I design the code in VBA in order to refer to a previously already newly created workbook in Excel? Mac Office 2011 VBA Word 指已打开的 Excel 工作簿 - Mac Office 2011 VBA Word refer to already opened Excel Workbook 希望Excel VBA从以下开始引用打开的工作簿 - Would like Excel VBA to refer to open workbook starting with Excel VBA中的语法:如何在“ = IF”函数中将表的列引用为范围 - Syntax in Excel VBA: how to refer to column of a table as a range in the “=IF” function 如何使用 VBA 复制用 VBA 打开的 Excel 工作簿的文件名,并在当前工作簿的另存为函数中使用它? - How to use VBA to copy a filename of an Excel workbook opened with VBA and use it in a save-as function for current workbook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM