VBA to create new worksheet and using values from original workbook for SaveCopyAs name and path

GuyGadois

Active Member
Joined
Jan 12, 2009
Messages
342
Office Version
  1. 2019
Platform
  1. Windows
I have the following VBA which works to take data from the Workbook, create a new workbook and paste that data. Now I want code to utilize the SaveCopyAs to automatically save the file with name (fname) and path (fpath) taken from the original Workbook. I just don't know how to do this. The declared values don't move to the newly created workbook. Any help is really appreciated.

Guy

Code:
Sub Export_G_MS()Dim R1 As Range, R2 As Range, R3 As Range, nxRw As Long
Set R1 = Range("Export_G_Portfolios")


    Dim Fname           As String
    Dim fPath           As String


    Fname = Range("Export_Name").Value 'getting export name from Workbook
    fPath = Range("FilePath").Value 'getting file path from Workbook


Dim sFileSaveName As Variant
    


On Error Resume Next
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With


  R1.SpecialCells(xlCellTypeVisible).Copy
  Set NewBook = Workbooks.Add
   Dim wb As Workbook


ActiveSheet.Name = "ExportMS"


  NewBook.Worksheets("ExportMS").Range("A1").PasteSpecial (xlPasteValues), Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False




On Error Resume Next


nxRw = Sheets("ExportMS").Cells(Rows.Count, "A").End(xlUp).Row + 1


For i = Sheets("ExportMS").Range("A1:A" & nxRw).Rows.Count To 1 Step -1
    If Sheets("ExportMS").Cells(i, "A").Value = "" Then Sheets("ExportMS").Cells(i, "A").EntireRow.Delete
Next i
    
Columns(2).NumberFormat = "##.#0%"
Columns(3).NumberFormat = "$##,#0"
Columns(4).NumberFormat = "##"


With Application
    .CutCopyMode = False
    .DisplayAlerts = True
    .ScreenUpdating = True
End With




'Save_As Now I want the new worksheet to automatically save in fPath using fname
         
     ActiveWorkbook.SaveCopyAs fPath & Fname & " - " & Format(Date, "YYYY.MM.DD") & ".xlsx"
     
     MsgBox "The File Has Been Saved as an Excel File" & vbNewLine & vbNewLine & "File Name: " & Fname & vbNewLine & "Destination: " & fPath & vbNewLine




End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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