If Error(Sheets(ws_str)) = True Then Exit Sub
返回运行时错误9。 我知道这张表不存在。如果工作表不存在,如何进行错误处理?
答案 0 :(得分:2)
尝试下面的一小段代码:
y+200
答案 1 :(得分:2)
这将解决您的问题:
Sub ErrorHandling()
On Error GoTo ExitSub
Dim ws As Worksheet
Set ws = Worksheets("NonExistingSheet") 'it will throw an error
MsgBox ("This won't be displayed")
ExitSub:
End Sub
基本上,在开始定义时,如果出现错误,您的代码应该恢复到哪里。为了满足您的要求,请将恢复点放在End Sub
之前,以便在发生错误时直接返回。
答案 2 :(得分:2)
Function SheetExists(sheetname as string) as boolean
On error goto whoops
dim ws as worksheet
set ws = worksheets(sheetname)
set ws = nothing
sheetexists = true
exit function
whoops:
sheetexists = false
end function