Error Handler: Nothing Happening

Status
Not open for further replies.

Hmerman

Board Regular
Joined
Oct 2, 2016
Messages
102
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
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top