简体   繁体   English

如何在VBA中获取当前工作表的路径?

[英]How to get the path of current worksheet in VBA?

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)?如何获取文件路径(只是目录)?

Application.ActiveWorkbook.Path仅用于路径本身(不带工作簿名称)或Application.ActiveWorkbook.FullName用于带有工作簿名称的路径。

Always nice to have:总是很高兴:

Dim myPath As String     
Dim folderPath As String 

folderPath = Application.ActiveWorkbook.Path    
myPath = Application.ActiveWorkbook.FullName

If you want to get the path of the workbook from where the macro is being executed - use Application.ThisWorkbook.Path .如果您想从执行宏的位置获取工作簿的路径 - 使用Application.ThisWorkbook.Path
Application.ActiveWorkbook.Path can sometimes produce unexpected results (eg if your macro switches between multiple workbooks). Application.ActiveWorkbook.Path有时会产生意想不到的结果(例如,如果您的宏在多个工作簿之间切换)。

最快的方式

path = ThisWorkbook.Path & "\"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM