如何在出错时暂停执行vba代码?

时间:2016-07-29 07:54:09

标签: vba excel-vba access-vba excel

我希望在出现错误后暂停执行 VBA 代码,并在纠正后继续执行?!因为我有很长的执行时间,所以它总是从一开始......

1 个答案:

答案 0 :(得分:1)

您需要使用错误处理程序。像

这样的东西
Sub test()
  Dim v As Variant, x As Integer 'etc etc
  On Error GoTo errorTrap
  'run your code here.  An example is below
  x = "hello" 'this will create an error since hello is not an integer
  MsgBox "finished"

  End 'ignore the error trap when done
errorTrap:
  Debug.Print Err.Description
  Stop 'stop here and figure out what is going on.
  'any other code needed to fix the error
  Resume Next
End Sub
在您的dims和其他设置之后直接在代码的开头

。 然后对于实际的errortrap,你可以在End Sub之前写这个。

整件事情看起来像这样。

augroup vimrc
    autocmd!
    autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) | NERDTreeToggle | wincmd l | endif
augroup END
相关问题