Avatar billede per2edb Guru
07. november 2018 - 11:02 Der er 11 kommentarer

Error handling VBA Code

Jeg har et meget kompleks program hvor det med vba kode er svært at afbryde programforløbet efter en fejl er opstået og gå til starten af programmet.

Jeg har i dag en klassisk error handler så jeg kan se fejlen - men herefter ønsker jeg at total stoppe programforløbet og gå til start

Kan i hjælpe
Avatar billede terry Ekspert
07. november 2018 - 11:31 #1
http://www.cpearson.com/excel/errorhandling.htm

As you can see from the link I gave above, if you handle the error in one procedure then the error is no longer there when you return to previous procedures. And this is obviously the problem if you want to stop executing code in all previous procedures.

Two solutions:
1 Only have error handling in first procedure
2 If you catch an error in any procedure then you can re-trigger the error in your error handler using

Err.Raise Err.Number

This send the same error back to the previous procedure where you hopefully handle it too
Avatar billede per2edb Guru
07. november 2018 - 12:29 #2
Jeg prøver Err.Raise Err.Number
Tak
Avatar billede Jørgen Kirkegaard Professor
08. november 2018 - 16:06 #3
"Jeg har i dag en klassisk error handler så jeg kan se fejlen - men herefter ønsker jeg at total stoppe programforløbet og gå til start"

Hvad mener du med, at du kan "se fejlen..og gå til start"? Jeg er bare nysgerrig, fordi jeg har samme udfordring og har valgt at sende en boolean retur gennem hele program-hierarkiet. Det er besværligt, især når den skal mosles sammen med de øvrige argumenter, jeg vil returnere. Jeg synes, at det er pænere end at fremprovokere en fejl for at kunne 'komme tilbage med den'.
Avatar billede terry Ekspert
12. november 2018 - 15:03 #4
"Jeg synes, at det er pænere end at fremprovokere en fejl for at kunne 'komme tilbage med den"

"!og har valgt at sende en boolean retur gennem hele program-hierarkiet."
But you still have to check the value of the Boolean in every procedure, and it also means you cant use a bollean as a return value for other things.

Nothing at all wrong throwing the same error if that's what it takes
Avatar billede terry Ekspert
12. november 2018 - 15:20 #5
"Jeg har i dag en klassisk error handler så jeg kan se fejlen - men herefter ønsker jeg at total stoppe programforløbet og gå til start"

Actually there may be an easier solution, but it depends what you mean with "....og gå til start"

If there is some code you want to execute in the first procedure then I think that one of the options in  #1 is correct. But if you want to terminate execution of all code then

just add

End

in your error handling routine

EG:

On Error GoTo TheEnd
    i = 1 / 0
     
    Exit Sub

TheEnd:

MsgBox "Error …." & Err.Number
End

End Sub
Avatar billede Jørgen Kirkegaard Professor
12. november 2018 - 16:29 #6
Terry: "But you still have to check the value of the Boolean in every procedure, and it also means you cant use a bollean as a return value for other things."
I use my own types to pass to and from functions/procedures. In case you need to, you could have a array with an error code in first place. In my opinion, this is "nicer".
Avatar billede terry Ekspert
12. november 2018 - 16:48 #7
"In my opinion, this is "nicer" ;-)

You did see the solution using End in #5? Maybe you could use that yourself
Avatar billede Jørgen Kirkegaard Professor
12. november 2018 - 18:17 #8
I did - but I don't like END in the middle of the procedure/function code either. Well, now per2edb has some options..
Avatar billede terry Ekspert
12. november 2018 - 21:04 #9
"but I don't like END in the middle of the procedure/function code either" you are hard to please arent you ;-)
Avatar billede Jørgen Kirkegaard Professor
12. november 2018 - 21:19 #10
Næh. Det er helt almindeligt, at eksekvering af kode afsluttes i bunden af proceduren. Jeg ville f.eks. aldrig skrive:
....
If boolvar then
End..
Else
Fortsæt kode
End If

End Sub/Function
---

I stedet ville jeg sørge for, en if-then-else-endif, som så afsluttedes i bunden:
If (not boolvar) then
Fortsæt kode
'else er tom
Endif

End Sub/Function

Men programmeringsstil kan jo diskuteres i det uendelige...
Avatar billede terry Ekspert
12. november 2018 - 21:30 #11
"Men programmeringsstil kan jo diskuteres i det uendelige" your right there ;-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester