Hi,
I've got some code that counts the number of *.csv files in a folder
This first loop works fine and gives me the number of files perfectly. It's the second part that brings in the problem.![]()
FolderPath = "my directory" path = FolderPath & "\*.csv" filename = Dir(path) Do While filename <> "" filecount = filecount + 1 filename = Dir() Loop filename = Dir(path) For filecount = 1 To filecount Debug.Print filename filename = Dir() Workbooks.Open ("mydirectory"/filename
The next section is a loop that opens each file, extracts the data, pastes it into a new file, saves and closes the newly created one, closes the opened one, and then loops onto the next file in the directory. With me?
However, when the "for filecount" loop calls the Dir(), it picks up the name of the second file in the directory, and not the first. so the sequence goes file #2,3,4,5.... but misses #1. It seems that I have to have the "filename = Dir(path)" between the Loop and For loop as otherwise there is no filename.
Can anyone help me to work out the order/naming to both count number of files and open all the files sequentially in a loop?
Thanks!!
Bookmarks