Connect and share knowledge within a single location that is structured and easy to search. 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? Use Application. Path for just the path itself without the workbook name or Application. FullName for the path with the workbook name. Path can sometimes produce unexpected results e. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to get the path of current worksheet in VBA? Ask Question. Asked 11 years, 8 months ago. Active 2 months ago. Viewed k times. Improve this question. TylerH Alex Gordon Alex Gordon Add a comment.
Active Oldest Votes. Improve this answer. BradC BradC 38k 12 12 gold badges 69 69 silver badges 90 90 bronze badges. Activeworkbook depends on which workbook is active. USe Thisworkbook. Both are useful in different situations, of course.
I took the original question to mean that the macro resided in an add-in workbook which would be ThisWorkbook. Should always be explicit - if it's this workbook, it should be application. If it's a workbook being opened, the name should be defined with a set, then application. Improve this answer. When you use Workbooks. Open "file. For developers using MS Access: Application. Path — duckboy You can also use the windows scripting shell object's. CurrentDirectory property.
Shell" Debug. Print wshell. CurrentDirectory But that should get the same result as just Debug. Print CurDir. It would seem likely that the ActiveWorkbook has not been saved Try CurDir instead. Monty Wild Monty Wild 3, 1 1 gold badge 19 19 silver badges 33 33 bronze badges. That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved. Path returns blank because you haven't saved your workbook yet.
To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again. This time it will not show blank, but will show you the path where it is located current folder I hope that helped.
Mohamed Tahir Mohamed Tahir 1 1 silver badge 4 4 bronze badges. If you really mean pure working Directory, this should suit for you. Amit Verma 7, 7 7 gold badges 31 31 silver badges 37 37 bronze badges. The goal of the question is to understand what the current path is programmatically and get that into a variable. Your solution simply opens windows explorer to show the user what path is which is not useful as it can not be subsequently used programmatically. Use these codes and enjoy it.
Add fso. GetFolder source 'obviously replace Do While queue. Remove 1 'dequeue ' For Each oSubfolder In oFolder. SubFolders queue. Files ' Print s Next End Sub.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Podcast Making Agile work for data science.
0コメント