VBA Excel Move a File to another folder if Filename contains a certain string

jme851

New Member
Joined
Jan 27, 2014
Messages
7
I need help with vba code, I need to move files that contain "CSC" in the file name to another folder, there .pdfs. The orginal folder will have pdfs names simlar to but not limited 123456_256_2022526 or CN_CSC 20220506 or CS_CSC 1234567890_222222 or C_CSC 23456158_222234. I only need to move the files with the CSC. I pulled some code from somethings my prior boss did before he left, it didn't work, I've search and just can't find anything that works. Hope someone can help 🙏 .

this is the code with variations of the If Trim. I've tried several different codes from online with no luck.
VBA Code:
Sub test()
  MyFolder = "C:\DMC\Prod\Archive\archive_lmr"
    MyFile = Dir(MyFolder & "*.PDF")
    Do While MyFile <> ""
      If Trim(Split(MyFile, "_")(1)) = "CSC " Then
       Name MyFolder & MyFile As "C:\DMC\Prod\Archive\NA Docs\" & MyFile
      End If
    Loop
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The orginal folder will have pdfs names simlar to but not limited 123456_256_2022526 or CN_CSC 20220506 or CS_CSC 1234567890_222222 or C_CSC 23456158_222234. I only need to move the files with the CSC
So, to clarify, are you saying that of those 4 you listed, you would want to move the final 3? That is:
(1) CN_CSC 20220506
(2) CS_CSC 1234567890_222222
(3) C_CSC 23456158_222234?

If so, how about changing the If Trim line to:
VBA Code:
If MyFile Like "*_CSC*" Then
This looks at the filename and if it has _CSC anywhere in it, it will move the file to the designated folder. The asterisks on either side are wildcards. Let me know how it goes.
 
Upvote 0
So, to clarify, are you saying that of those 4 you listed, you would want to move the final 3? That is:
(1) CN_CSC 20220506
(2) CS_CSC 1234567890_222222
(3) C_CSC 23456158_222234?

If so, how about changing the If Trim line to:
VBA Code:
If MyFile Like "*_CSC*" Then
This looks at the filename and if it has _CSC anywhere in it, it will move the file to the designated folder. The asterisks on either side are wildcards. Let me know how it goes.
Thank you for the suggestion, It didn't work..
 
Upvote 0
At all?
Are the characters lower case? Can you confirm the question in my reply above?
This is a pretty simple task, so the solution will be pretty straightforward - it just needs a bit more info.
 
Upvote 0
Just looking at your code above again - do the filenames start off with archive_lmr or is that the folder name?
Because if it's a folder name, then you're missing a \ after it.
 
Upvote 0

Forum statistics

Threads
1,224,300
Messages
6,177,755
Members
452,798
Latest member
mlance617

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