Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking to see if SaveAs was successful VBA

Tags:

excel

vba

What kind of statement do I need to check whether or not a SaveAs operation was successful in vba?

Sub saveBookAs()
    wb.SaveAs fileName:="newFile"

End Sub
like image 925
Adjit Avatar asked Sep 14 '25 06:09

Adjit


1 Answers

You don't need a statement to check if the workbook was saved or not :) If the Save As process fails then that line will error out automatically :)

Having said that if you want to check if a file exists or not you can always use the DIR function. DIR Returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.

Syntax

Dir[(pathname[, attributes])]

Check VBA help for more details.

EDIT

Another tip.

If you don't want your code to break, then you can also use proper Error Handling.

For example

Sub Sample()
    On Error GoTo Whoa

    ActiveWorkbook.Save SomeFilePathAndName

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub
like image 155
Siddharth Rout Avatar answered Sep 17 '25 00:09

Siddharth Rout