Create new workbooks from names in list

sfos83

New Member
Joined
Mar 2, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I was simply hoping to simply create new workbooks (xlsx) each named after a cell in column A, and save them to a drive and close them.
It will need to loop through each line and assign the new workbook that cell value as a name; I've tried all variations of code, and it usually stumbles at the 'SaveAs' step for multiple reasons.

- The length of the list in column A will be variable (between say, 1 and 20 files)
- I'd like to save it in directory "C:\Users\Steve\Desktop\PA Test Folder\Test Files\OneDrive Files\"

1657068983930.png


Could someone please help? I'm going crazy, thanks!
Ss
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to Mr. Excel...

Try the below...

VBA Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet, i As Long, x As String
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
End With
Set ws = ActiveSheet
With ws
    For i = .Range("A" & Rows.Count).End(3).Row - 1 To 1 Step -1
    x = .Cells(i + 1, "A").Value
        If .Cells(i + 1, "A") <> .Cells(i, "A") Then
            Workbooks.Add
            ActiveWorkbook.SaveAs "C:\Users\Steve\Desktop\PA Test Folder\Test Files\OneDrive Files\" & x & ".xlsx"
            ActiveWorkbook.Close True
        End If
        .Activate
    Next i
End With
With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .Calculation = xlCalculationManual
End With
End Sub
 
Upvote 0
Thanks so much for your reply, works a charm!

Steve
Spoke too soon!
Worked a couple of times, now when it runs it closes the sheet in which it is running?
I've not changed any of the code, which is odd....
 
Upvote 0
Spoke too soon!
Worked a couple of times, now when it runs it closes the sheet in which it is running?
I've not changed any of the code, which is odd....

I retested now... I even extended the amount of files to be created to about 30.... On my side it creates the files and keeps original sheet open from where filenames are located...

As you stated you did not change any of the code... So do you have any other code / macros running together with this?
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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