Moving files based on file type and filename length using For loop

jirica

New Member
Joined
Sep 2, 2017
Messages
4
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
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top