Copy files with a certain keyword to another folder with the same keyword

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello...

I have a list of source folder and its target folder, but with the code below it only copy to the folder in the second row.
How to make it loop through the list?

The source folder is in column CA and the target folder is in column CB.

Thank you in advance.

VBA Code:
 Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
    Dim c As Range, rPatterns As Range
    
    
    sSrcFolder = ActiveSheet.Cells(2, 79)
    sTgtFolder = ActiveSheet.Cells(2, 80)
    
    Set rPatterns = ActiveSheet.Range("BU2:BU20").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
    
    MsgBox "File copy completed"
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi feni1388. It seems like this will work. Change this part of your code. HTH. Dave
VBA Code:
Dim cnt As Integer
cnt = 1
For Each c In rPatterns
cnt = cnt + 1
sSrcFolder = ActiveSheet.Cells(cnt, 79)
sTgtFolder = ActiveSheet.Cells(cnt, 80)
sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
'etc
 
Upvote 0
Solution
Hi feni1388. It seems like this will work. Change this part of your code. HTH. Dave
VBA Code:
Dim cnt As Integer
cnt = 1
For Each c In rPatterns
cnt = cnt + 1
sSrcFolder = ActiveSheet.Cells(cnt, 79)
sTgtFolder = ActiveSheet.Cells(cnt, 80)
sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
'etc

Thank you Dave.
It works perfectly.....
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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