Good day all,
Hope you are well.
I have written a Error Handler based on analystcave.com' info on proper techniques. The Error Handler is for multiple errors, Error 11 (division by zero) and other errors. Two different nmessages are displayed in the event of errors.
The problem is even if I divide by zero on purpose (to check if the Error Handler works), there is no error message: the code seems to Exit Sub and no message or anything.
My code is formulas to calculate calibration of tractors and pumps for given values in textboxes on a Userform 1.
Here is my code with the error handler:
Private Sub CommandButton4_Click()
'----------------------------------------------------------------------------------------------------------
'CALCULATES THE CALIBRATION OF TRACTORS AND PUMPS FOR IRRIGATION.
'----------------------------------------------------------------------------------------------------------
Dim errMsg As String
On Error GoTo ErrorHandler
'in Textbox 8
If Arec8 = "" And Arec5.Value <> "" Then
Arec8.Value = Arec5 / 60
End If
'in Textbox 6
If Arec6 = "" And Arec1 = "" And Arec7.Value <> "" And Arec8.Value <> "" Then
Arec6 = Arec8 * Arec7
Else
If Arec6 = "" And Arec4.Value <> "" And Arec1.Value <> "" Then
Arec6 = (Arec4 * 100 * Arec1) / 10000
End If
End If
'in Textbox 1
If Arec1 = "" And Arec6.Value <> "" And Arec4.Value <> "" Then
Arec1 = (Arec6 * 10000) / (Arec4 * 100) 'If the value in textbox Arec4 is zero I get a error 11 which should be Handled by the error handler below
End If
'in Textbox 7
If Arec7 = "" And Arec6.Value <> "" And Arec8.Value <> "" Then
Arec7 = Arec6 / Arec8 'If the value in textbox Arec4 is zero I get a error 11 which should be Handled by the error handler below
End If
Arec1 = Format(Arec1.Value, "0")
Arec3 = Format(Arec3.Value, "0")
Arec4 = Format(Arec4.Value, "0.00")
Arec5 = Format(Arec5.Value, "0.00")
Arec6 = Format(Arec6.Value, "0.00")
Arec7 = Format(Arec7.Value, "0.00")
Arec8 = Format(Arec8.Value, "0.00")
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 11:
GoTo DivideByZeroError
Case Default:
GoTo OtherError
End Select
DivideByZeroError:
Debug.Print "Cannot divide by zero!"
Err.Clear
Exit Sub
OtherError:
errMsg = "Error number: " & Str(Err.Number) & vbNewLine & _
"Source: " & Err.Source & vbNewLine & _
"Description: " & Err.Description
Debug.Print errMsg
Err.Clear
Exit Sub
End Sub
The text in red is where a zero would give a error 11.
Can someone please help to make the Error Handler work?
Regards
Herman
Hope you are well.
I have written a Error Handler based on analystcave.com' info on proper techniques. The Error Handler is for multiple errors, Error 11 (division by zero) and other errors. Two different nmessages are displayed in the event of errors.
The problem is even if I divide by zero on purpose (to check if the Error Handler works), there is no error message: the code seems to Exit Sub and no message or anything.
My code is formulas to calculate calibration of tractors and pumps for given values in textboxes on a Userform 1.
Here is my code with the error handler:
Private Sub CommandButton4_Click()
'----------------------------------------------------------------------------------------------------------
'CALCULATES THE CALIBRATION OF TRACTORS AND PUMPS FOR IRRIGATION.
'----------------------------------------------------------------------------------------------------------
Dim errMsg As String
On Error GoTo ErrorHandler
'in Textbox 8
If Arec8 = "" And Arec5.Value <> "" Then
Arec8.Value = Arec5 / 60
End If
'in Textbox 6
If Arec6 = "" And Arec1 = "" And Arec7.Value <> "" And Arec8.Value <> "" Then
Arec6 = Arec8 * Arec7
Else
If Arec6 = "" And Arec4.Value <> "" And Arec1.Value <> "" Then
Arec6 = (Arec4 * 100 * Arec1) / 10000
End If
End If
'in Textbox 1
If Arec1 = "" And Arec6.Value <> "" And Arec4.Value <> "" Then
Arec1 = (Arec6 * 10000) / (Arec4 * 100) 'If the value in textbox Arec4 is zero I get a error 11 which should be Handled by the error handler below
End If
'in Textbox 7
If Arec7 = "" And Arec6.Value <> "" And Arec8.Value <> "" Then
Arec7 = Arec6 / Arec8 'If the value in textbox Arec4 is zero I get a error 11 which should be Handled by the error handler below
End If
Arec1 = Format(Arec1.Value, "0")
Arec3 = Format(Arec3.Value, "0")
Arec4 = Format(Arec4.Value, "0.00")
Arec5 = Format(Arec5.Value, "0.00")
Arec6 = Format(Arec6.Value, "0.00")
Arec7 = Format(Arec7.Value, "0.00")
Arec8 = Format(Arec8.Value, "0.00")
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 11:
GoTo DivideByZeroError
Case Default:
GoTo OtherError
End Select
DivideByZeroError:
Debug.Print "Cannot divide by zero!"
Err.Clear
Exit Sub
OtherError:
errMsg = "Error number: " & Str(Err.Number) & vbNewLine & _
"Source: " & Err.Source & vbNewLine & _
"Description: " & Err.Description
Debug.Print errMsg
Err.Clear
Exit Sub
End Sub
The text in red is where a zero would give a error 11.
Can someone please help to make the Error Handler work?
Regards
Herman
Last edited: