0

Below you can read the problematic part of my VBA macro code. I try to loop through all files in a folder. When I run the code, it does not open anything, instead, it returns "runtime error 1004", it can not find the file. The interesting thing is, that it displays the name of the file precisely. I did check, the file is in the correct folder, extension, name, everything is correct. What is more, the file name was found by the Dir function, not typed by me, which makes it complete nonsense to me. I introduced a message box line before the open command and it displayed the exact name of the first file of the folder looped through.

ControlFile = Dir(ControlFolder & "\", vbReadOnly)

Do While ControlFile <> ""
    Workbooks.Open (ControlFile)
    ControlFile = Dir
Loop

Ferenc

Community
  • 1
  • 1
  • I do not see you [calling `Dir` repeatedly](https://stackoverflow.com/a/12058116/11683) which is required to enumerate a folder. I do see you examining the contents of the `ControlFile` variable repeatedly that apparently never changes. – GSerg May 03 '18 at 19:51
  • Thanks, you are right. I corrected the code accordingly. Yet, the error message is the same. – user9734243 May 03 '18 at 20:10
  • Yes, it may be a duplicate, sorry. I am new to this forum and not used to it yet, but a few minutes ago I read an answer for this question of mine (or was it another question recommended by the site???) which said that the Dir function returns the filename only and to open it I have to create the full path. I did it by typing ControlFolder & "\" & in front of "controlfile" in the open method and it worked fine. So it must be a duplicate, but I missed it as I concentrated on the Dir function. – user9734243 May 03 '18 at 20:42
  • When your question is marked as a duplicate it does not mean you did something bad. Different people may word the same problem differently, so accumulating a bunch of differently worded questions that point to the same underlying problem and linking them all together is good for the site because it increases the chances that the next person will find the answer using their keywords. The answer you saw was mine, I deleted it after I realised your problem about relative file names is already covered in another question, and voted to close as a duplicate instead. – GSerg May 03 '18 at 20:47
  • O.K., Thank you. – user9734243 May 04 '18 at 04:29

0 Answers0