Use VBA to save as a new workbook with a specific file name and file location

woah

New Member
Joined
May 10, 2023
Messages
24
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I am looking for a VBA that can save the "Column A to E" of "sheet 1" to another new workbook with a specific file name (refer to cell G1) and a specific file folder.

Please kindly advise. Thank you very much.

Save.png
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello @woah
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.​


Specify on this line of the macro the folder you want.
sPath = "C:\trabajo\files\"


VBA Code:
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
  
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
  
  sh.Range("A:E").Copy wb.Sheets(1).Range("A1")
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution
Hello @woah
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.​


Specify on this line of the macro the folder you want.
sPath = "C:\trabajo\files\"


VBA Code:
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
 
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
 
  sh.Range("A:E").Copy wb.Sheets(1).Range("A1")
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------

Thank you so much!!! It works perfectly. I only have one more question. If I want to save the worksheet in the new worksheet with the name "Locker", what should I do? It now appears as "Sheet 1" only. Thank you!!!!
 
Upvote 0
If I want to save the worksheet in the new worksheet with the name "Locker"
Please try this:

Rich (BB code):
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
  
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
  
  sh.Range("A:E").Copy wb.Sheets(1).Range("A1")
  wb.Sheets(1).Name = "Locker"
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False
End Sub

Have a nice day ;)
 
Upvote 0
Please try this:

Rich (BB code):
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
 
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
 
  sh.Range("A:E").Copy wb.Sheets(1).Range("A1")
  wb.Sheets(1).Name = "Locker"
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False
End Sub

Have a nice day ;)

Sorry, I just came up with another question. If I only want to save the range content (A10:E), how can I do it? But the range sometimes contain more or less data.
 
Upvote 0
EDIT:


Sorry, I just came up with another question. If I only want to save the part of the content (A7:E), how can I do it? But the range sometimes contain more or less data, and some column not always contain data. Can I use Column C as reference to copy content? So that, I want it to copy up to the last row of the range (Column C) with content.
 
Upvote 0
If I only want to save the part of the content (A7:E),

Use this:

VBA Code:
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
  
  sh.Range("A7:E" & sh.Range("C" & Rows.Count).End(3).Row).Copy wb.Sheets(1).Range("A1")
  wb.Sheets(1).Name = "Locker"
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False

  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

Have a nice day :cool:
 
Upvote 0
Use this:

VBA Code:
Sub saveNewWorkbook()
  Dim sh As Worksheet
  Dim wb As Workbook
  Dim sPath As String
 
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
 
  Set sh = ThisWorkbook.Sheets("Sheet1")
  Set wb = Workbooks.Add(xlWBATWorksheet)
  sPath = "C:\trabajo\files\"     'specific file folder
 
  sh.Range("A7:E" & sh.Range("C" & Rows.Count).End(3).Row).Copy wb.Sheets(1).Range("A1")
  wb.Sheets(1).Name = "Locker"
  wb.SaveAs sPath & sh.Range("G1").Value & ".xlsx"
  wb.Close False

  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

Have a nice day :cool:

Sorry that once I changed the range into "A14:G" and used Column "D" as the reference to extract data in last row, the new workbook still copy the whole worksheet. Also, some cells become (#REF!) and format is totally mess up.

May I know the reason? Thank you
 
Upvote 0
I only have one more question. If I want to save the worksheet in the new worksheet with the name "Locker"
If I only want to save the range content (A10:E)
If I only want to save the part of the content (A7:E) ... copy up to the last row of the range (Column C)
once I changed the range into "A14:G" and used Column "D" ... Also, some cells become (#REF!) and format is totally mess up.
May I know the reason?

It's hard to give you a diagnosis of what's going on, if you're continually changing the requirement. And that's fine, but if you're modifying the macro, I really don't know what you changed if you don't put the modified macro here.

On the other hand, if you have formulas, we need to know if you want to paste the formulas or the values.

I suggest that you carefully analyze what you need and once you are clear about your objective, come back here and create a new thread with all your needs. That way we can deliver a macro that works for you.

This is very important: in the new thread you put how your sheet is and how you want the sheet in the new book.

Cordially
Dante Amor
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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