Cancel option when saving

eli_m

Board Regular
Joined
Jun 2, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

With the code at the bottom how can I make it so it does NOT do:
VBA Code:
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula

If the user hits cancel when saving?

VBA Code:
Sub SaveQuoteAsWorkbook()

Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52

End Sub

Thanks in advance!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
What does the SaveQuoteAsWorkbook Sub get called from?
 
Upvote 0
What does the SaveQuoteAsWorkbook Sub get called from?
A button:
1678846020096.png


Just to be clear, I am talking about Cancel button button when saving a document:

1678846210648.png
 
Upvote 0
Try changing this:
VBA Code:
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

To This:
VBA Code:
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")
 
Upvote 0
Try changing this:
VBA Code:
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

To This:
VBA Code:
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")

I did this:
VBA Code:
Sub SaveQuoteAsWorkbook()

Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52

End Sub

but it still clears H4 when I press Cancel because of:
VBA Code:
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula

I only want it to Copy then paste when I press "Save" - I want it not to run the above code if I press Cancel here:
1678847700630.png
 
Upvote 0
Try this
VBA Code:
Sub SaveQuoteAsWorkbook()

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
Else
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula
End IF

End Sub
 
Upvote 0
Try this
VBA Code:
Sub SaveQuoteAsWorkbook()

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
Else
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula
End IF

End Sub
Sorry. Not tested. I may have to change the IF statement a little.
 
Upvote 0
Yeah, my bad. This should work.
VBA Code:
Sub SaveQuoteAsWorkbook()

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula
Else
Exit Sub
End IF

End Sub
 
Upvote 0
Yeah, my bad. This should work.
VBA Code:
Sub SaveQuoteAsWorkbook()

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = "Quote " & Range("B66")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))

If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula
Else
Exit Sub
End IF

End Sub

Thanks for your help but I am getting this error:
1678848959485.png
 
Upvote 0
Small change in Syntax
VBA Code:
If FileSaveName <> "False" Then 
      ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
       Range("H4").Copy
       Range("H4").PasteSpecial xlPasteValuesAndNumberFormats ' Removes H4 formula
Else
      Exit Sub
End IF
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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