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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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