PDA

View Full Version : Solved: Universal error trap



danovkos
03-12-2009, 07:52 AM
hi all,
please exist any universal command or code, which switch off any error show?
i think, if is any code, which i write to any macro and it do, that error (debuger) will never display. If will be any macro assigned to button and if this macro doesnt works, it doesnt show error, only doenst nothing?

thank you very much

BrianMH
03-12-2009, 10:00 AM
Just add to the top of the code

On error exit sub

or exit function if its a function

Bob Phillips
03-12-2009, 10:26 AM
This is a VERY bad practice



On Error Resume Next

BrianMH
03-12-2009, 10:28 AM
Both of them are bad practice or just mine? TBH I think both of them are probably a bad thing since your just pretending the errors don't exist. I just put exit sub because he said "do nothing" so I assume he wants it to just stop doing anything.

Bob Phillips
03-12-2009, 10:33 AM
No I was referring to what the OP wants to do, ignore all errors. We can give them what they want, but that doesn't make it right :dunno

CreganTur
03-12-2009, 10:37 AM
Ignoring all errors is like setting yourself on fire and then turning up the air conditioning because you're hot; it doesn't deal with the source of the problem.

The best thing the Op could do is actually trap the errors. Exiting on error is just going to confuse and frustrate any Users, since the program won't work and they won't be able to understand why.

BrianMH
03-12-2009, 10:48 AM
Oh yeah. I actually WANT my users to let me know if they come across an error. It usually means I didn't think of a scenario in which it could be used. I know that my code is never going to be perfect and its always good to find out the bugs. I mean I wouldn't want users to spend a year of corrupting data before finding a bug.

CreganTur
03-12-2009, 10:52 AM
Oh yeah. I actually WANT my users to let me know if they come across an error. It usually means I didn't think of a scenario in which it could be used. I know that my code is never going to be perfect and its always good to find out the bugs. I mean I wouldn't want users to spend a year of corrupting data before finding a bug.

Not only because of that, but there are many instances when the user is going to screw something up or do something in the wrong order no matter how much you try to railroad their actions through good design. Errors are a good way to remind them of things they may have forgotten.

danovkos
03-14-2009, 04:46 AM
i understund, thank you for your advise.

How can I do something, what will show to user, that his last action was wrong/or make an error. So if he click on my button and my macro will be show an error, is better to show only some splash or tooltip message - "now is it error" and continue without other change. Because for my users is very bad if its stops the macro and open the debuger. They dont know, what they have to do.

Meybe something like this but not msg box, because this it necessary to accept.

On error goto wrong:
wrong:
msgbox, "error - now it is error"
exit sub

BrianMH
03-14-2009, 04:50 AM
on error goto errorhandler

' your code here

exit sub

errorhandler:
msgbox(err.num & " " & err.description)
exit sub


This will pop up a message box with the error number and error description but will avoid the debug prompt.

mdmackillop
03-14-2009, 04:56 AM
Sub DoError()

On Error GoTo ErrH:

'Do some things, error in this case
Cells(0, 0).Select
MsgBox Selection.Address

'Exit sub before error handler
Exit Sub
'Error handler
ErrH:
MsgBox Err & " : " & Err.Description
End Sub

danovkos
03-14-2009, 05:38 AM
yours examples looks good, i will try it on monday...
thank you very much for your help

danovkos
03-16-2009, 12:24 AM
hi,
i tried your code, but it doesnt work for my macro.
This code search the value from cell in sheet 1 in sheet 2. But if i use autofilter with some condition in sheet2 (here it search), and the value is not displayed, it show an error and debugger in part:

Cells.Find(What:=pval, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

this is the code..but i want something, what will works for all macros..but it isnt possible or?
thx

Sub FindValue1()
'
On Error GoTo errorhandler

pval = Range("B1").Value
pval2 = Range("E1").Value
On Error GoTo 0
If pval = "here id" Then
GoTo koniec:
ElseIf pval2 = "is not in DB" Then
GoTo koniec:
ElseIf pval <> "here id" Then
Sheets("view").Select
Range("B1").Select
Sheets("vývoj").Select
Range("A1").Select
On Error GoTo 0
Cells.Find(What:=pval, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
koniec:
Exit Sub

errorhandler:
MsgBox (Err.num & " " & Err.Description)
Exit Sub
End Sub

mdmackillop
03-16-2009, 01:15 AM
This cancels Goto ErrH
On Error GoTo 0

mdmackillop
03-16-2009, 01:24 AM
When using find as you have, you get an error when nothing is found and you try to activate it. This can be avoided if you set a range to the result, and check if the range is Nothing.

Test1 below will error, Test2 will not

Sub Test1()
Dim c As Range
Set c = Columns(1).Find("Data33")
c.Activate
End Sub


Sub Test2()
Dim c As Range
Set c = Columns(1).Find("Data33")
If Not c Is Nothing Then c.Activate
End Sub

danovkos
03-16-2009, 11:55 PM
Ok, but how can i implementet your code to my?
i tried it, but it doesnt works. I like the second vs „do nothing“.

i tried it in this form but it is wrong :(


Sub FindValue1()
'
Dim c As Range

pval = Range("B1").Value
pval2 = Range("E1").Value
On Error GoTo 0
If pval = "text" Then
GoTo koniec:
ElseIf pval2 = "text2" Then
GoTo koniec:
ElseIf pval <> "text" Then
Sheets("view").Select
Range("B1").Select
Sheets("vývoj").Select
Range("A1").Select
On Error GoTo 0
Set c = Columns(1).Find("Data33")
If Not c Is Nothing Then c.Activate
Cells.Find(What:=pval, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
On Error GoTo 0
End If
koniec:
End Sub

mdmackillop
03-17-2009, 01:37 AM
Always use Option Explicit
Analyse your code to avoid unnecessary steps and get rid of the verbiage.

Option Explicit
Sub DoStuff()
Dim c As Range
Dim pval, pval2

pval = Range("B1").Value
pval2 = Range("E1").Value
If pval2 = "text2" Then Exit Sub

If pval <> "text" Then
Set c = Sheets("vývoj").Cells.Find(What:=pval, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not c Is Nothing Then Application.Goto c
End If
End Sub

danovkos
03-17-2009, 02:51 AM
YES, this works great.
thank you very much