VBA code to Copy File Based on Keywords in File Name - Revisited

kisstake

New Member
Joined
Dec 22, 2022
Messages
2
Good day! Is there a way to modify this code, and not copy files but instead moving them, something like cut from old location and paste to the new location? Many thanks in advance.
Below code was provided more than 10 years ago in another Post thread entitled "VBA code to Copy File Based on Keywords in File Name".
It works great, but I would like to move the files instead of copying them.
Many thanks in advance! Wishing you happy holidays!

Sub CopyFiles_Containing()
Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
Dim c As Range, rPatterns As Range
Dim bBad As Boolean

sSrcFolder = ActiveSheet.Cells(4, 3)
sTgtFolder = ActiveSheet.Cells(5, 3)

Set rPatterns = ActiveSheet.Range("E4:E2000").SpecialCells(xlConstants)
For Each c In rPatterns
sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
If sFilename = "" Then
c.Interior.ColorIndex = 3
bBad = True
Else
While sFilename <> ""
FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
sFilename = Dir()
Wend
End If
Next c
If bBad Then MsgBox "Some files were not found. " & _
"These were highlighted for your reference."
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to MrExcel forums - please post code inside VBA tags - click the VBA icon in the message editor.

I would like to move the files instead of copying them.

Untested; change:
VBA Code:
FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
to:
VBA Code:
Name sSrcFolder & sFilename As sTgtFolder & sFilename
 
Upvote 0
Welcome to MrExcel forums - please post code inside VBA tags - click the VBA icon in the message editor.



Untested; change:
VBA Code:
FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
to:
VBA Code:
Name sSrcFolder & sFilename As sTgtFolder & sFilename
Many thanks for the fast reply, amazing, it worked like charm! I will use the VBA icon next time, sorry, I didn't know that. Wishing you all the best!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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