17

I have a macro-enabled WorkBook. I need to specify the current folder in which the macro-enabled file is present as the path. I tried setting

path = ActiveWorkbook.Path

and

path = CurDir()

but neither of these work for me. Any idea on this?

9
  • ActiveWorkbook.Path is the right way to get the path. How and where are you calling this code? Commented Apr 18, 2012 at 18:32
  • Activeworkbook.path doens't exist until document is saved once does it? Commented Apr 18, 2012 at 18:32
  • 5
    If it's the file with the running macro then you want ThisWorkbook.Path Commented Apr 18, 2012 at 18:56
  • +1 Tim Williams. Yup :) hence it is important to know how and where is he calling this code from? Commented Apr 18, 2012 at 18:59
  • @Siddharth - I was going on "the current folder in which the macro enabled file is present" Commented Apr 18, 2012 at 19:02

2 Answers 2

32

If the path you want is the one to the workbook running the macro, and that workbook has been saved, then

ThisWorkbook.Path

is what you would use.

Sign up to request clarification or add additional context in comments.

Comments

7

I thought I had misunderstood but I was right. In this scenario, it will be ActiveWorkbook.Path

But the main issue was not here. The problem was with these 2 lines of code

strFile = Dir(strPath & "*.csv")

Which should have written as

strFile = Dir(strPath & "\*.csv")

and

With .QueryTables.Add(Connection:="TEXT;" & strPath & strFile, _

Which should have written as

With .QueryTables.Add(Connection:="TEXT;" & strPath & "\" & strFile, _

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.