LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default VBA Question - Canceling a 'Save As' command from a user form

Hi V-Man,

A few things you can do here. If the user clicks Cancel, then
GetSaveAsFilename will return False. So if you set up your variable as a
Variant, you can check for that. If the user is prompted to overwrite and
clicks No, VBA will raise a runtime error. So you can ignore that error if
you'd like.

Here's an example:

Private Sub SaveButton_Click()
Dim vName As Variant

On Error GoTo ErrHandler

vName = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls, Text Files" & _
"(*.txt), *.txt, All Files (*.*), *.*")

If Not vName = False Then
ActiveWorkbook.SaveAs Filename:=CStr(vName)
End If

ExitRoutine:
Exit Sub
ErrHandler:
If Err.Number < 1004 Then
MsgBox "Unexpected error." & vbLf & CStr(Err.Number) & _
": " & Err.Description, vbExclamation, "Error"
End If
Resume ExitRoutine
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


V-Man wrote:
Hi all,

I have built a user form in VBA that allows a user to enter and save
data in a template. This data is saved on Sheet 1 of my workbook. I
created a 'save' button on my template that works just fine until the
user is asked if they want to replace a current copy of the file. If
the user clicks 'no', then I get one of two responses: 1) it saves my
file as 'FALSE' or it generates a 1004 run time error.

Here is the code for my save button:

Private Sub SaveButton_Click()
fName = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls, Text Files (*.txt),
*.txt, All Files (*.*), *.*")
ActiveWorkbook.SaveAs Filename:=fName

End Sub

Any suggestions?


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Form question ah Excel Discussion (Misc queries) 1 November 5th 07 10:40 AM
User form Command Buttons jhyatt Excel Discussion (Misc queries) 3 September 25th 07 04:28 PM
User form and Command button Marilyn Excel Discussion (Misc queries) 3 May 9th 07 12:50 AM
Command Button on User form for Printing Anthony Slater Excel Discussion (Misc queries) 2 September 7th 05 02:01 PM
User form question Gareth[_3_] Excel Programming 1 October 29th 03 06:07 PM


All times are GMT +1. The time now is 08:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"