lasvegasbuffet
New Member
- Joined
- Feb 12, 2025
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
I'm trying to use a nested Do Loop but I get the error Run-time error ‘5’: Invalid procedure call or argument on line myFile1 = Dir
This code will be for more than just getting file names in message boxes, that's just there to test functionality. If I can get this error solved then I'll be able to go further. Thanks in advance!
VBA Code:
Sub LoopAllExcelFilesInFolder()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim myPath1 As String
Dim myPath2 As String
Dim myFile1 As String
Dim myFile2 As String
Dim myExtension1 As String
Dim myExtension2 As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
myPath1 = "C:\Cost Report\R&S Error Report\Numerator Overview\"
myPath2 = "C:\Cost Report\R&S Error Report\Numerator Overview Missing Par Con\"
myExtension1 = "*.xls*"
myExtension2 = "*.xls*"
myFile1 = Dir(myPath1 & myExtension1)
myFile2 = Dir(myPath2 & myExtension2)
Do While myFile1 <> ""
Set wb1 = Workbooks.Open(fileName:=myPath1 & myFile1)
DoEvents
msgBox ActiveWorkbook.Name
Do While myFile2 <> ""
Set wb2 = Workbooks.Open(fileName:=myPath2 & myFile2)
DoEvents
msgBox ActiveWorkbook.Name
wb2.Close SaveChanges:=True
DoEvents
myFile2 = Dir
Loop
wb1.Close SaveChanges:=True
DoEvents
myFile1 = Dir
Loop
msgBox "Task Complete!"
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
This code will be for more than just getting file names in message boxes, that's just there to test functionality. If I can get this error solved then I'll be able to go further. Thanks in advance!