Cancel Button on file save

L

lwm

Using this code


NewName = InputBox(prompt:="File Name and Directory", Default:=FName)

ActiveWorkbook.SaveAs Filename:=NewName, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False, AddToMru:=True

When the user presses the cancel key the macro fails. What do I add to get
it to just end the sub.

Thanks
 
D

Dave Peterson

You could just overwrite any existing file:

newname = inputbox(...)

application.displayalerts = false
activeworkbook.saveas ...
application.displayalerts = true

to allow the user to cance if there's an existing file:

newname = inputbox(...)
on error resume next
activeworkbook.saveas ...
if err.number <> 0 then
msgbox "something bad happened"
err.clear
end if
on error goto 0

'keep going....

==========
You may want to look at application.getsaveasfilename in VBA's help. It can
eliminate a lot of typing errors by the user and a lot of validation checks for
you the developer. (Instead of an inputbox.)
 
L

lwm

Thanks Dave

I am doing a begginer class for non techical peolpe on simple macros so I am
trying to use recorded macro's and them make minor easy to explain changes.
:)

Which means it is not so easy for me sometimes.
 
D

Dave Peterson

One common task is to open an existing file or to save a file with a new name.

You can record the action, but that recorded macro will have the names embedded
into the recorded macro.

One of the things you may want to include in your task is a way of getting the
name of the file to open (application.getopenfilename) and a way of getting the
name of file to save (application.getsaveasfilename).

Those commands can be incorporated into recorded code that really increases the
functionality of the macro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Simplify save code 11
Save with ref. to cell A1 2
Saving a workbook to an iKnow portal 2
Hiding an Excel file using VBA 1
Save as marco 3
Save As File Format 1
Recording a 'Save As...' 4
SaveAs to existing file 5

Top