Checking to see if SaveAs was successful VBA

2019-07-11 02:26发布

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

1条回答
可以哭但决不认输i
2楼-- · 2019-07-11 03:05

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
查看更多
登录 后发表回答