Good day VBA Experts,
Thank you so much in advance,
I have managed to create a code consisting of 3 macros that works. That is One main macro and calling the other two. All 3 have almost the same structure except for the If parts. Please see below my initial code below that errors out. I would like to settle for this one and not have 3 macros if you could help me fix the error.
Sub MoveFiles()
Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strSourceFolder As String
Dim strDestFolder As String
Application.ScreenUpdating = False
strSourceFolder = "C:\Users\jirica\Desktop\SAP"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strSourceFolder)
For Each objMyFile In objMyFolder.Files
'3 files all the same upto this point
'Main macro start
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 11 Then
strDestFolder = strSourceFolder & "" & "AI" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
'Main macro ends 2nd macro starts
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 16 Then '****This line Runtime error 53 file not found****'
strDestFolder = strSourceFolder & "" & "MOON" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
2nd macro ends 3rd Macro starts
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 17 Then
strDestFolder = strSourceFolder & "" & "Claims" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
3rd macro ends
'3 files all the same from below onwards
Next objMyFile
Set objFSO = Nothing
Set objMyFolder = Nothing
Application.ScreenUpdating = True
End Sub
Thanks again
jirica
Thank you so much in advance,
I have managed to create a code consisting of 3 macros that works. That is One main macro and calling the other two. All 3 have almost the same structure except for the If parts. Please see below my initial code below that errors out. I would like to settle for this one and not have 3 macros if you could help me fix the error.
Sub MoveFiles()
Dim objFSO As Object
Dim objMyFolder As Object
Dim objMyFile As Object
Dim strSourceFolder As String
Dim strDestFolder As String
Application.ScreenUpdating = False
strSourceFolder = "C:\Users\jirica\Desktop\SAP"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMyFolder = objFSO.GetFolder(strSourceFolder)
For Each objMyFile In objMyFolder.Files
'3 files all the same upto this point
'Main macro start
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 11 Then
strDestFolder = strSourceFolder & "" & "AI" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
'Main macro ends 2nd macro starts
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 16 Then '****This line Runtime error 53 file not found****'
strDestFolder = strSourceFolder & "" & "MOON" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
2nd macro ends 3rd Macro starts
If InStr(objMyFile.Name, ".xl") > 0 And Len(objMyFile.Name) = 17 Then
strDestFolder = strSourceFolder & "" & "Claims" & Format(Range("d6"), "YYYY")
If Len(Dir(strDestFolder, vbDirectory)) = 0 Then
MkDir strDestFolder
End If
objFSO.MoveFile Source:=strSourceFolder & "" & objMyFile.Name, Destination:=strDestFolder & "" & objMyFile.Name
End If
3rd macro ends
'3 files all the same from below onwards
Next objMyFile
Set objFSO = Nothing
Set objMyFolder = Nothing
Application.ScreenUpdating = True
End Sub
Thanks again
jirica