Copy and rename file from list

Wil Moosa

Well-known Member
Joined
Aug 11, 2002
Messages
893
I have one file; Mother.xls

Mother.xls has on sheet(6) a list with names of all the employees. I want to create as many copies of Mother.xls as found on the list on sheet(6) each file carrying the name as mentioned on the list.

The following code does the trick for me:
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Code:
Sub CopyToEachName()
    Dim cell As Range
     ThisWorkbook.Save
    For Each cell In Worksheets("Sheet6").Range("A2", _
        Worksheets("Sheet6").Range("A2").End(xlDown))
        ThisWorkbook.SaveCopyAs _
            ThisWorkbook.Path & Application.PathSeparator & cell.Value & ".xls"
    Next cell
End Sub
 
Upvote 0
I already solved your problem. This method is better than SaveAs. It replaces the files if they exist already.
 
Upvote 0
I do see the potential of your code... and it did work. Any practical suggestion for deleting sheet6 in the copies?
 
Last edited:
Upvote 0
A For .. Next loop in a personal macro keeps active after you save the file with another name.

First store the name of the list in a variable before you delete sheet(6). SaveAs xxx.xls, close xxx.xls and re-open the original file.
 
Upvote 0
Code:
Sub Sheet6CopyToEachName()
    Dim sWB0 As String
    Dim a, v
    
    ThisWorkbook.Save
    sWB0 = ThisWorkbook.FullName
     
     a = Worksheets("Sheet6").Range("A2", _
        Worksheets("Sheet6").Range("A2").End(xlDown))
        
    ThisWorkbook.Save
    Application.DisplayAlerts = False
    Worksheets("Sheet6").Delete
    Application.DisplayAlerts = True
    
    For Each v In a
        ThisWorkbook.SaveCopyAs _
            ThisWorkbook.Path & Application.PathSeparator & v & ".xls"
    Next v
    
    Application.DisplayAlerts = False
    Workbooks.Open sWB0
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,697
Messages
6,167,702
Members
452,132
Latest member
Steve T

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