?
Solved

Adding error code to Excel macro

Posted on 2007-10-18
3
Medium Priority
?
260 Views
Last Modified: 2013-12-25
How do I put error code in this macro.  If the file already exsists and I select "NO" do not overright I get an error where to macro should just terminate.

Sub SaveQuit()
      Dim Response As Integer
      ThisFile = Range("k19").Value
      ActiveWorkbook.SaveAs Filename:=ThisFile, FileFormat:=52
      ' Displays a message box with the yes and no options.
            Response = MsgBox(prompt:="Quit Program?", Buttons:=vbYesNo)
      ' If statement to check if the yes button was selected.
      If Response = vbYes Then
         Application.Quit
      Else
         ' The no button was selected.
         End If
   End Sub
0
Comment
Question by:mweidner
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 20100325
Hi mweidner,

Try something like this....

Sub SaveQuit()
    Dim thisfile As String: thisfile = Range("k19").Value
    If Dir(thisfile) <> "" Then
        'file exists
        ActiveWorkbook.SaveAs Filename:=thisfile, FileFormat:=52
        ' Displays a message box with the yes and no options.
        If MsgBox(prompt:="Quit Program?", Buttons:=vbYesNo) = vbYes Then
        'Yes selected
            Application.Quit
        Else
            ' The no button was selected.
           
        End If
    Else
        'file does not exist
       
    End If
End Sub

Regards,

Wayne
0
 
LVL 4

Expert Comment

by:jindalankush
ID: 20100600
try it
 If Dir(ThisFile) <> "" Then Kill (ThisFile) then
   If MsgBox(prompt:="Quit Program?", Buttons:=vbYesNo) = vbYes Then
         Application.Quit
  else
    ActiveWorkbook.SaveAs Filename:=ThisFile, FileFormat:=52
 end if
end if
0
 
LVL 4

Expert Comment

by:jindalankush
ID: 20100611
sorry on last thread try it
 If Dir(ThisFile) <> "" Then then
   If MsgBox(prompt:="Quit Program?", Buttons:=vbYesNo) = vbYes Then
         Application.Quit
  else
       Kill (ThisFile)
    ActiveWorkbook.SaveAs Filename:=ThisFile, FileFormat:=52
 end if
end if
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question