VBA: Save current workbook with new name

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need to save the current workbook with a new name and then run a macro on just the new workbook. I would also like to close the new workbook, but keep the original workbook open.

Here is what I have so far. The new workbook is getting saved with the macro running on the new workbook, but both workbook close. Should I name the current workbook in the code?

The current workbook is Master.xlsm

Code:
Sub Save_it()
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim FilePath As String
    FilePath = ActiveWorkbook.Path & Application.PathSeparator
    Set Destwb = ActiveWorkbook
    With Destwb
        .SaveAs FilePath & "Input.xlsm", FileFormat:=52
        Call DeleteSheets
        .Close SaveChanges:=True
    End With
End Sub
Code:
Sub DeleteSheets()
    Application.DisplayAlerts = False
    Sheets("Sheet2").Delete
    Sheets("Sheet3").Delete
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Not tested

Code:
Sub Save_it()
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim WS As Worksheet
    Dim FilePath As String
    FilePath = ActiveWorkbook.Path & Application.PathSeparator
    ThisWorkbook.SaveCopyAs (FilePath & "Input.xlsm")
    Set Destwb = Application.Workbooks.Open(Filename:=FilePath & "Input.xlsm")
    With Destwb
        For Each WS In .Worksheets
            Application.DisplayAlerts = False
            Select Case WS.Name
            Case "Sheet2", "Sheet3"
                WS.Delete
            End Select
            Application.DisplayAlerts = True
        Next WS
        .Close SaveChanges:=True
    End With
End Sub
 
Upvote 0
Thank you rlv01. That is absolutely perfect. Just want I was needing.
 
Upvote 0
Sweet riv01! The alerts might come outside the loop?. I'll save a copy of that. Thanks. Dave
ps. frygirl I'm glad U arrive at a solution
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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