Loop not looping through files in DIR (Outer Loop)
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
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
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.5k73270
17.5k73270
asked Nov 11 at 5:31
alowflyingpig
1287
1287
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
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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,795112139
6,795112139
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 |
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 |
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 |
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
1,1271213
1,1271213
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