0

I've received the following code to add to a closed code (protected by password) so an error can be catch.

On Error Resume Next: Err.Clear

Application.SetOption "Error Trapping", 2

strLine = Application.Run("Comdinheiro.xlam!ExecutaURL_Cliente2", "Fundamentalista3-0-" _

& strData & "-0-" & strCODIGO_CVM & "-" & strDF & "-IFRS-comdinheiro-1")

If Err.Number <> 0 Then

    Sleep i * 500 'Espera 0.5 us antes de executar novamente a função com problema

    Err.Clear

    On Error GoTo 0

    strLine = Application.Run("Comdinheiro.xlam!ExecutaURL_Cliente2", "Fundamentalista3-0-" _

    & strData & "-0-" & strCODIGO_CVM & "-" & strDF & "-IFRS-comdinheiro-1")

End If

1- Application.setOption "Error Trapping", 2 : Is there any Erro trapping option in VBA? I couldn't find it online.

2- On Error Resume next: Err.clear : I've seen On Error Resume next, or On Error Resume but never something like that.

Can someone explain me points 1 and 2.

Ihidan
  • 558
  • 1
  • 7
  • 25
  • If you are confused with this part `: Err.Clear` in point 2, colon is just the symbol of new line in VBA. This line is equal for compiler to: `On Error Resume Next` and `Err.Clear` in the next line (I don't know how to break line in comment in S.O.). – mielk Jul 30 '15 at 14:47

2 Answers2

0

Error handling two ways.

Check values for nulls and stuff that cause common errors.

Actual handling.

   On Error goto SkipLine

   'Some code that might cause error


SkipLine:
    'Code to report or deal with error.
    msgbox Err.Description & " " & Err.Number & " " & Err.Name

For "on error resume next" i would caution you to be VERY careful using this.

 On Error Resume next

 'Some code that might cause error

 On Error goto 0

The On Error goto 0 is what will set it back to raising errors.

MatthewD
  • 6,719
  • 5
  • 22
  • 41
0

Check out this questions. It should give you all you need to know:

Good Patterns For VBA Error Handling

Community
  • 1
  • 1
LimaNightHawk
  • 6,613
  • 3
  • 41
  • 60