adding error handling to UDF

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
How can i better write this code and add error handling?

Code:
Function TotalStainless(EasyorHard As Double, Thickness As Double, NumOfPierces As Double, HoleCutLength As Double, TotalPerimeter As Double)
    Dim PierceSecs As Double
    Dim HolesInchPerMin As Double
    Dim PerimInchPerMin As Double
    Dim Efficiency As Double
    Dim part1 As Double
    Dim part2 As Double
    Efficiency = Sheets("Standard").Range("AA4")
    If Thickness = 0 Then
        PierceSecs = 0.1
        HolesInchPerMin = 280
        PerimInchPerMin = 325
    Else
        If Thickness = 0.035 Then
            PierceSecs = 0.2
            HolesInchPerMin = 175
            PerimInchPerMin = 200
        Else
            If Thickness = 0.048 Then
                PierceSecs = 0.2
                HolesInchPerMin = 175
                PerimInchPerMin = 200
            Else
                If Thickness = 0.06 Then
                    PierceSecs = 0.3
                    HolesInchPerMin = 120
                    PerimInchPerMin = 140
                Else
                    If Thickness = 0.075 Then
                        PierceSecs = 0.75
                        HolesInchPerMin = 100
                        PerimInchPerMin = 100
                    Else
                        If Thickness = 0.105 Then
                            PierceSecs = 0.75
                            HolesInchPerMin = 100
                            PerimInchPerMin = 100
                        Else
                            If Thickness = 0.135 Then
                                PierceSecs = 0.3
                                HolesInchPerMin = 70
                                PerimInchPerMin = 90
                            Else
                                If Thickness = 0.18 Then
                                    PierceSecs = 0.3
                                    HolesInchPerMin = 57
                                    PerimInchPerMin = 65
                                Else
                                    If Thickness = 0.25 Then
                                        PierceSecs = 0.3
                                        HolesInchPerMin = 57
                                        PerimInchPerMin = 65
                                    Else
                                        If Thickness = 0.312 Then
                                            PierceSecs = 1
                                            HolesInchPerMin = 30
                                            PerimInchPerMin = 35
                                        Else
                                            If Thickness = 0.375 Then
                                                PierceSecs = 5
                                                HolesInchPerMin = 25
                                                PerimInchPerMin = 29
                                            Else
                                                If Thickness = 0.437 Then
                                                    PierceSecs = 4
                                                    HolesInchPerMin = 25
                                                    PerimInchPerMin = 29
                                                Else
                                                    If Thickness = 0.5 Then
                                                        PierceSecs = 9
                                                        HolesInchPerMin = 15
                                                        PerimInchPerMin = 13
                                                    Else
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
        If EasyorHard = 1 Then
            part1 = (PierceSecs / Efficiency * NumOfPierces)
            part2 = (HoleCutLength / HolesInchPerMin) / Efficiency + (TotalPerimeter / PerimInchPerMin) / Efficiency
        Else
            part1 = (PierceSecs / Efficiency * NumOfPierces) * (1 + Sheets("Standard").Range("J4"))
            part2 = ((HoleCutLength / HolesInchPerMin) / Efficiency + (TotalPerimeter / PerimInchPerMin) / Efficiency) * (1 + Sheets("Standard").Range("J4"))
        End If
        part1 = Round(part1, 1)
        part2 = Round(part2, 1)
        TotalStainless = part1 + part2
End Function
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I would recommend writing all those horrible nested ifs like
Code:
   Select Case Thickness
      Case 0
         PierceSecs = 0.1
         HolesInchPerMin = 280
         PerimInchPerMin = 325
      Case 0.035, 0.048
         PierceSecs = 0.2
         HolesInchPerMin = 175
         PerimInchPerMin = 200
      Case 0.06
         PierceSecs = 0.3
         HolesInchPerMin = 120
         PerimInchPerMin = 140
      Case 0.075, 0.105
         PierceSecs = 0.75
         HolesInchPerMin = 100
         PerimInchPerMin = 100
   End Select
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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