vba 如何在VBA中获取当前工作表的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2813925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the path of current worksheet in VBA?
提问by l--''''''---------''''''''''''
I wrote a macro as an add-in, and I need to get the path of the current worksheet on which it is being executed. How do I do this? How do I get the file path (just the directory)?
我写了一个宏作为加载项,我需要获取正在执行它的当前工作表的路径。我该怎么做呢?如何获取文件路径(只是目录)?
回答by BradC
Use Application.ActiveWorkbook.Pathfor just the path itself (without the workbook name) or Application.ActiveWorkbook.FullNamefor the path withthe workbook name.
使用Application.ActiveWorkbook.Path的只是路径本身(没有工作簿的名称),或者Application.ActiveWorkbook.FullName该路径与工作簿的名称。
回答by Alex22
Always nice to have:
总是很高兴:
Dim myPath As String
Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path
myPath = Application.ActiveWorkbook.FullName
回答by avalanche1
If you want to get the path of the workbook from where the macro is being executed - use Application.ThisWorkbook.Path.Application.ActiveWorkbook.Pathcan sometimes produce unexpected results (e.g. if your macro switches between multiple workbooks).
如果要从执行宏的位置获取工作簿的路径,请使用Application.ThisWorkbook.Path. Application.ActiveWorkbook.Path有时会产生意想不到的结果(例如,如果您的宏在多个工作簿之间切换)。

