Help with error trapping placement

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a formula that obtains it values from a userform and a spreadsheet. The problem is if any value is missing from either the userform or mainly the spreadsheet, I should get a Runtime-error 11 'Division by zero, which I do, as expected. So I wrote this little snippet of code, within a Sub, to to do something else when that runtime error fires. The problem is no matter where I place the code it still displays the Runtime-error message and not my code. So I am assuming its an issue of where the code is located. However, I noticed no matter where I put it, it still does execute. I tried putting in the beginning of the sub after I declared my variables, which to me was way to soon because the formula is later in the code. So I tried after the formula and Runtime error still displays. So I'm stuck for the moment. Any help would be greatly appreciated. If there is any error in my code, please let me know how I can fix it also. Thank you.

Code:
Sub ErrorTrap11()   
     If Err.Number = 11 Then
        MsgBox Chattemfrm.cmbPrdCde.Value & " not found or is missing values in Product list. Please add missing product to the product list or fill in missing data in order to continue.", vbOKOnly + vbCritical + vbDefaultButton1, "Missing Information"
        Worksheets(Chattemfrm.cmbSDPFLine.Value).Activate
        Unload Me
        frmAddProduct.Show
        Exit Sub
     End If
End Sub

Error occurs in the first line of code when I debugged it:
Code:
textValUp = ((txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16
textValDown = ((txtbxdz.Value) / txtDz / txtCs) - 0.5 + 1E-16
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Code:
On Error Resume Next
Insert that line in the line immediately above the line where the code fails
- VBA ignores the error and moves to next instruction which is to exit the userform

or another way would be to prevent the error by testing the value first

Code:
    If Chattemfrm.cmbPrdCde.Value <> 0 Then
       run the required code
    Else
        MsgBox Chattemfrm.cmbPrdCde.Value & " not found or is missing values in Product list. Please add missing product to the product list or fill in missing data in order to continue.", vbOKOnly + vbCritical + vbDefaultButton1, "Missing Information"
        Worksheets(Chattemfrm.cmbSDPFLine.Value).Activate
        Unload Me
        frmAddProduct.Show
        Exit Sub
     End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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