• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Error when change directory

Hi Chandoo,

I want to ask if there is any rule before we change directory in VBA?
I have just made a code and this code run smoothly. (I have put source and destination file into one folder). There is nothing error at all.
But when I just copy the that folder to place anywhere else, there is error message (as In attached file) even i change directory in the code. Then I copy that folder to place in the same place as before, but the error message still exist.
Do you know why it become like this?

Thanks,
Chanthan
 

Attachments

  • Error Msg.jpg
    Error Msg.jpg
    269.7 KB · Views: 19
Hi !

As it depends where the error occurs,
as you forgot to post your code as per forum rules,
as we are not mind readers,
all we can say there is an error on the file path/name !
You can easily check during execution in debug mode via Locals windows …

Tip : see what exactly returns Dir VBA function.
Just check the string path when you open a file
as current directory is not the path of files to open …

So considering previous points :
Do you know why it become like this?
Bad logic, bad code …
 
You must have a file named "A". Excel assumed it is an xlsx file type since you tried to open it as a workbook. Your code should check the file type before using Workbooks.Open.

Since you did not use ChDir() or add the path, it is just luck that Workbooks.Open works as you think it should.

I would recommend some changes in your code. I seldom find need for Select, Activate, Selection, and such.
 
Back
Top