Macro to copy files from existing space to new folder

CombatWombat

New Member
Joined
Oct 16, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello and thank you in advance!

I want to begin by saying I am in no way an expert and I appreciate some of the incredible answers that are on this site.

I have a workbook with multiple sheets that I use to help identify useful files from old jobs that can be used in new ones.

I have set up the workbook to list all files in folders and subfolders in one sheet, which I then sort manually, and copy those I identify as important onto a second sheet.

What I am looking for is a Macro that will take a list of file paths A2:A100 on a different sheet "sheet 2" and copy those listed files into a new folder that I list in A1 "sheet2" which the macro could hopefully create.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the forum,

Perhaps the below will help:
VBA Code:
Sub test()
    Dim sPath As String, dPath As String
    Dim tVar As Variant, fName As String
    Dim rCell As Range

    dPath = Sheet2.Range("A1").Value ' path must end with a \
    
    If Dir(dPath, vbDirectory) = "" Then
        MkDir dPath
    End If
    
    For Each rCell In Sheet2.Range("A2:A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row)
        sPath = rCell.Value
        tVar = Split(sPath, "\")
        fName = tVar(UBound(tVar))
        Name sPath As dPath & fName
    Next rCell
End Sub
 
Upvote 0
Welcome to the forum,

Perhaps the below will help:
VBA Code:
Sub test()
    Dim sPath As String, dPath As String
    Dim tVar As Variant, fName As String
    Dim rCell As Range

    dPath = Sheet2.Range("A1").Value ' path must end with a \
   
    If Dir(dPath, vbDirectory) = "" Then
        MkDir dPath
    End If
   
    For Each rCell In Sheet2.Range("A2:A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row)
        sPath = rCell.Value
        tVar = Split(sPath, "\")
        fName = tVar(UBound(tVar))
        Name sPath As dPath & fName
    Next rCell
End Sub
Thank you for the super quick reply.

Unfortunately I am getting a "error 9 subscript out of range" for the fname line. I won't even pretend to know what that means but any further help would be amazing.
 
Upvote 0
Upvote 0
I see, the range is not contiguous, try it with the below:
VBA Code:
Sub test()
    Dim sPath As String, dPath As String
    Dim tVar As Variant, fName As String
    Dim rCell As Range

    dPath = Sheet2.Range("A1").Value ' path must end with a \
    
    If Dir(dPath, vbDirectory) = "" Then
        MkDir dPath
    End If
    
    For Each rCell In Sheet2.Range("A2:A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row)
        If rCell.Value <> "" Then
            sPath = rCell.Value
            tVar = Split(sPath, "\")
            fName = tVar(UBound(tVar))
            Name sPath As dPath & fName
        End If
    Next rCell
End Sub
 
Upvote 0
So the error code is gone now but the code doesn't appear to do anything (I press run F5 and nothing happens)

Thank you again.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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