run time error fix

HonestMan

New Member
Joined
May 12, 2011
Messages
4
Hi guys

I am using the below code to save the workbook to the desktop by click of a button. When the button is clicked, it saves it to the desktop. After saving, a message box appears asking 'do you want to save the file'. If I click 'yes' or 'no' it works fine. But after it is saved to the desktop, if i reopen and click on save to desktop button a message box appears again asking 'do you want to save the file'. If I click 'yes' the code works fine but if I click 'no' I get a run time error. can any please tell me how to fix this. Thanks in advance

Private Sub CommandButton1_Click()
If Range("D5") = "" Then
MsgBox "Please enter yyy before saving the file."
Else
ActiveWorkbook.SaveAs Filename:=CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & Range("D5")
Range("x50").Value = "xxx"
ActiveWindow.Close
End If
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this:
Code:
Private Sub CommandButton1_Click()
If Range("D5") = "" Then
MsgBox "Please enter yyy before saving the file."
Else
Range("x50").Value = "xxx"
ActiveWorkbook.SaveAs Filename:=CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & Range("D5")
ActiveWorkbook.Close SaveChanges:=False
End If
End Sub
 
Upvote 0
It seems to be working for me. But a quick question... is there a reason you have it saving regardless of whether "Yes" or "No" or even "Cancel" is selected?
 
Upvote 0
actually I dont want it to save when clicking no or cancel but all I want is discard the changes made and dont want it to show the run time error thats it:)
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,652
Members
452,934
Latest member
mm1t1

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