Loop not looping through files in DIR (Outer Loop)










0














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









share|improve this question



















  • 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















0














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









share|improve this question



















  • 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













0












0








0







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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:07









usr2564301

17.5k73270




17.5k73270










asked Nov 11 at 5:31









alowflyingpig

1287




1287







  • 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












  • 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







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












2 Answers
2






active

oldest

votes


















2














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





share|improve this answer
















  • 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


















0














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





share|improve this answer






















  • that didnt work
    – alowflyingpig
    Nov 11 at 8:00










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
);



);













draft saved

draft discarded


















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









2














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





share|improve this answer
















  • 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















2














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





share|improve this answer
















  • 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













2












2








2






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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













0














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





share|improve this answer






















  • that didnt work
    – alowflyingpig
    Nov 11 at 8:00















0














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





share|improve this answer






















  • that didnt work
    – alowflyingpig
    Nov 11 at 8:00













0












0








0






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo