Macro for making a copy of the open workbook.

Luke1690

Board Regular
Joined
Jul 26, 2022
Messages
121
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Ive been looking online and have seen a few possible ways to do this, thought id see what you lot know before trying them.

1) I'm after a macro that will save a copy of the current open workbook in a different location.
2) I'm then going to add my wipe data feature in the same macro so i would like a warning message before the macro runs asking along the line of (are you sure)

is this possible guys any snippets of code would be great full.

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this to make a copy of current open workbook:
VBA Code:
Sub SaveCopy()
    'Declare a variable to hold the current workbook
    Dim wb As Workbook
    'Set the variable equal to the current open workbook
    Set wb = ActiveWorkbook
    
    'Prompt the user for the location to save the copy of the workbook
    Dim savePath As String
    savePath = Application.GetSaveAsFilename( _
        InitialFileName:=wb.Name, _
        FileFilter:="Excel Files (*.xlsx), *.xlsx")
    
    'Save a copy of the workbook to the specified location
    wb.SaveCopyAs savePath
End Sub

Code to ask user if they are sure?
VBA Code:
Dim response As Integer

response = MsgBox("Are you sure?", vbYesNo, "Confirm")

If response = vbYes Then
  ' Insert your code here that you want to run after the user confirms
Else
  ' Insert code here to handle the case where the user does not confirm
End If
 
Upvote 0
Solution
Try this to make a copy of current open workbook:
VBA Code:
Sub SaveCopy()
    'Declare a variable to hold the current workbook
    Dim wb As Workbook
    'Set the variable equal to the current open workbook
    Set wb = ActiveWorkbook
   
    'Prompt the user for the location to save the copy of the workbook
    Dim savePath As String
    savePath = Application.GetSaveAsFilename( _
        InitialFileName:=wb.Name, _
        FileFilter:="Excel Files (*.xlsx), *.xlsx")
   
    'Save a copy of the workbook to the specified location
    wb.SaveCopyAs savePath
End Sub

Code to ask user if they are sure?
VBA Code:
Dim response As Integer

response = MsgBox("Are you sure?", vbYesNo, "Confirm")

If response = vbYes Then
  ' Insert your code here that you want to run after the user confirms
Else
  ' Insert code here to handle the case where the user does not confirm
End If
Amazing response, thankyou so much.
 
Upvote 0
Thankyou also, i will look into this to better my knowledge.

You are welcome but do be aware SaveCopyAs does not have the File Format option so you cannot save the file in another format using this method - it does what it says, Saves A Copy of the workbook

Dave
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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