VBA- Save 2 worksheets as separate files. File name and path based on cell values.

ExceLoki

Well-known Member
Joined
Dec 13, 2021
Messages
541
Office Version
  1. 365
Platform
  1. Windows
hey all, i'm trying to save the worksheets MOM and SLN each as separate workbook with file name and file path based on separate cell values on the parameters sheet.
thanks in advance!
-----------
VBA Code:
Sub CopyItOver()
Path = B15
Filename = B14
  Set NewBook = Workbooks.Add
  Workbooks("Reporting.xlsm").Worksheets("MOM").Range("A1:AO1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial
  
  NewBook.SaveAs Filename:=Path & Filename & ".xlsx", FileFormat:=xlNormal
  
End Sub
-----------


Reporting.xlsm
AB
1ParameterValue
2EmployeeID
3
41/1/2019
5*1st day of the previous month
6DatabaseServer
7Database
8
91/1/2020
10
11
12
13
14MOMTermFileNameMOMTermFileName_YYYYMMDD.xlsx
15MOMTermPathC:\Temp\
Parameters

-----------

1642598604351.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
i'm currently trying this to include both sheets. before i only had one sheet in the vba.

VBA Code:
Sub CopyItOver2()

Set Path = Sheets("Parameters").Range("B15")
Set Filename = Sheets("Parameters").Range ("B14")

  Set NewBook = Workbooks.Add
  Workbooks("Reporting.xlsm").Worksheets("MOM").Range("A1:AO1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial
  
  NewBook.SaveAs FileLocation:=Path.Value & _
Filename.Value & ".xls", _
FileFormat:=xlNormal, CreateBackup:=False
Application.Display Alerts = True

Set Path = Sheets("Parameters").Range("B13")
Set Filename = Sheets("Parameters").Range ("B12")

  Set NewBook2 = Workbooks.Add
  Workbooks("Reporting.xlsm").Worksheets("SLN").Range("A1:AO1000").Copy
  NewBook2.Worksheets("Sheet1").Range("A1").PasteSpecial
  
  NewBook2.SaveAs FileLocation:=Path.Value & _
Filename.Value & ".xls", _
FileFormat:=xlNormal, CreateBackup:=False
Application.Display Alerts = True

End Sub
 
Upvote 0
coworker helped me with this:

VBA Code:
Sub CopyItOver2()

Dim Path As String
Dim filename As String
Dim Path2 As String
Dim filename2 As String

Path = Sheets("Parameters").Range("B15")
filename = Sheets("Parameters").Range("B14")
Path2 = Sheets("Parameters").Range("B17")
filename2 = Sheets("Parameters").Range("B16")

  Set NewBook = Workbooks.Add
  Workbooks("ReportingPDS.xlsm").Worksheets("MOM").Range("A1:AO1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial
  
ActiveWorkbook.SaveAs filename:=Path & _
filename & ".xls", _
FileFormat:=xlNormal, CreateBackup:=False

  Set NewBook2 = Workbooks.Add
  Workbooks("ReportingPDS.xlsm").Worksheets("SLN").Range("A1:AO1000").Copy
  NewBook2.Worksheets("Sheet1").Range("A1").PasteSpecial
  
ActiveWorkbook.SaveAs filename:=Path2 & _
filename2 & ".xls", _
FileFormat:=xlNormal, CreateBackup:=False

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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