Checking to see if SaveAs was successful VBA

excel, vba

Solution

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

Problem

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 ```

Original source