Loop not looping through files in DIR (Outer Loop)
up vote
0
down vote
favorite
I have written a code that works, a DIR loop that loops through CSV files, does code, then calls next until the end. The problem started when I added another loop within this loop, DIR loop to call PDF's...
I have stepped through the code and the problem is the first outer loop not calling the next CSV (the PDF loop works fine). I've done some research but can't make any adjustments work.
Any assistance is appreciated. I don't mind not being given the answer if you could show me where to look to resolve.
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next 'oFile
'Loop through each file in the folder
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
excel vba
|
show 5 more comments
up vote
0
down vote
favorite
I have written a code that works, a DIR loop that loops through CSV files, does code, then calls next until the end. The problem started when I added another loop within this loop, DIR loop to call PDF's...
I have stepped through the code and the problem is the first outer loop not calling the next CSV (the PDF loop works fine). I've done some research but can't make any adjustments work.
Any assistance is appreciated. I don't mind not being given the answer if you could show me where to look to resolve.
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next 'oFile
'Loop through each file in the folder
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
excel vba
2
I don't thinkDIR
function could be called recursively.. Using File System Object would be better option.
– Ahmed AU
Nov 11 at 5:43
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have written a code that works, a DIR loop that loops through CSV files, does code, then calls next until the end. The problem started when I added another loop within this loop, DIR loop to call PDF's...
I have stepped through the code and the problem is the first outer loop not calling the next CSV (the PDF loop works fine). I've done some research but can't make any adjustments work.
Any assistance is appreciated. I don't mind not being given the answer if you could show me where to look to resolve.
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next 'oFile
'Loop through each file in the folder
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
excel vba
I have written a code that works, a DIR loop that loops through CSV files, does code, then calls next until the end. The problem started when I added another loop within this loop, DIR loop to call PDF's...
I have stepped through the code and the problem is the first outer loop not calling the next CSV (the PDF loop works fine). I've done some research but can't make any adjustments work.
Any assistance is appreciated. I don't mind not being given the answer if you could show me where to look to resolve.
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next 'oFile
'Loop through each file in the folder
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
excel vba
excel vba
edited Nov 11 at 12:07
usr2564301
17.2k73270
17.2k73270
asked Nov 11 at 5:31
alowflyingpig
987
987
2
I don't thinkDIR
function could be called recursively.. Using File System Object would be better option.
– Ahmed AU
Nov 11 at 5:43
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50
|
show 5 more comments
2
I don't thinkDIR
function could be called recursively.. Using File System Object would be better option.
– Ahmed AU
Nov 11 at 5:43
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50
2
2
I don't think
DIR
function could be called recursively.. Using File System Object would be better option.– Ahmed AU
Nov 11 at 5:43
I don't think
DIR
function could be called recursively.. Using File System Object would be better option.– Ahmed AU
Nov 11 at 5:43
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
oFile
already has the file path included with it. You should set the target file path in oFolder
and loop within that directory.
If you only want .pdf
files then keep the If oFile.Name
line - otherwise if you want all files then delete the if statement.
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
add a comment |
up vote
0
down vote
A Working Solution?
Change the line
oFSO.GetBaseName(oFile) = FiName2
to
FiName2 = oFSO.GetBaseName(oFile)
EDIT:
Is this your final solution then?
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim sPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim FiName2 As String
sPath = ThisWorkbook.Path & "csv_macro"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
For Each oFile In oFolder.Files
FiName2 = oFSO.GetBaseName(oFile)
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
that didnt work
– alowflyingpig
Nov 11 at 8:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
oFile
already has the file path included with it. You should set the target file path in oFolder
and loop within that directory.
If you only want .pdf
files then keep the If oFile.Name
line - otherwise if you want all files then delete the if statement.
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
add a comment |
up vote
2
down vote
accepted
oFile
already has the file path included with it. You should set the target file path in oFolder
and loop within that directory.
If you only want .pdf
files then keep the If oFile.Name
line - otherwise if you want all files then delete the if statement.
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
oFile
already has the file path included with it. You should set the target file path in oFolder
and loop within that directory.
If you only want .pdf
files then keep the If oFile.Name
line - otherwise if you want all files then delete the if statement.
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
oFile
already has the file path included with it. You should set the target file path in oFolder
and loop within that directory.
If you only want .pdf
files then keep the If oFile.Name
line - otherwise if you want all files then delete the if statement.
Set oFolder = oFSO.GetFolder(ThisWorkbook.Path & "csv_macro")
For Each oFile In oFolder.Files
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
answered Nov 11 at 7:58
K.Dᴀᴠɪs
6,047112140
6,047112140
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
add a comment |
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
1
1
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
BOOM!!! Worked! Thx mate. Spent 2 days at this.. You've helped me learn and understand. My macros are getting better each time due to people like you who help.
– alowflyingpig
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
Glad we were able to get it resolved for you
– K.Dᴀᴠɪs
Nov 11 at 8:06
add a comment |
up vote
0
down vote
A Working Solution?
Change the line
oFSO.GetBaseName(oFile) = FiName2
to
FiName2 = oFSO.GetBaseName(oFile)
EDIT:
Is this your final solution then?
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim sPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim FiName2 As String
sPath = ThisWorkbook.Path & "csv_macro"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
For Each oFile In oFolder.Files
FiName2 = oFSO.GetBaseName(oFile)
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
that didnt work
– alowflyingpig
Nov 11 at 8:00
add a comment |
up vote
0
down vote
A Working Solution?
Change the line
oFSO.GetBaseName(oFile) = FiName2
to
FiName2 = oFSO.GetBaseName(oFile)
EDIT:
Is this your final solution then?
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim sPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim FiName2 As String
sPath = ThisWorkbook.Path & "csv_macro"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
For Each oFile In oFolder.Files
FiName2 = oFSO.GetBaseName(oFile)
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
that didnt work
– alowflyingpig
Nov 11 at 8:00
add a comment |
up vote
0
down vote
up vote
0
down vote
A Working Solution?
Change the line
oFSO.GetBaseName(oFile) = FiName2
to
FiName2 = oFSO.GetBaseName(oFile)
EDIT:
Is this your final solution then?
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim sPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim FiName2 As String
sPath = ThisWorkbook.Path & "csv_macro"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
For Each oFile In oFolder.Files
FiName2 = oFSO.GetBaseName(oFile)
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
A Working Solution?
Change the line
oFSO.GetBaseName(oFile) = FiName2
to
FiName2 = oFSO.GetBaseName(oFile)
EDIT:
Is this your final solution then?
Sub Coles_claims_consolidation()
'Coles Claims Import Macro
Dim sPath As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim FiName2 As String
sPath = ThisWorkbook.Path & "csv_macro"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
For Each oFile In oFolder.Files
FiName2 = oFSO.GetBaseName(oFile)
If oFile.Name Like "*.pdf" Then
FileCopy oFile, ThisWorkbook.Path & "" & FiName2 & ".pdf"
End If
Next oFile
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
End Sub
edited Nov 25 at 9:20
answered Nov 11 at 7:54
VBasic2008
644213
644213
that didnt work
– alowflyingpig
Nov 11 at 8:00
add a comment |
that didnt work
– alowflyingpig
Nov 11 at 8:00
that didnt work
– alowflyingpig
Nov 11 at 8:00
that didnt work
– alowflyingpig
Nov 11 at 8:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246115%2floop-not-looping-through-files-in-dir-outer-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I don't think
DIR
function could be called recursively.. Using File System Object would be better option.– Ahmed AU
Nov 11 at 5:43
I could add another DIR loop as long as its not inside a previous loop? In the code above, I could move the pdf DIR loop to after where i save ColesAllWB (at end of code)
– alowflyingpig
Nov 11 at 5:46
it may solve problem. Give it a try
– Ahmed AU
Nov 11 at 5:48
I have trimmed the code to the above. I researched scripting and the code works. DIR loop works, however the only part that doesn't work now is the filecopy. the debug prints. no errors are given
– alowflyingpig
Nov 11 at 7:41
What does filecopy doesn't work mean? What is/isn't happening?
– QHarr
Nov 11 at 7:50