Code Cleanup - Multiple If-Thens

CV355

New Member
Joined
Dec 16, 2016
Messages
10
This is probably a really basic question, but what is the most efficient way to restructure this code to eliminate all of the if-thens? I'm thinking "With" but it did not work when I tried. This is part of a dynamic userform that automatically calculates required cycle times based on production data and efficiencies. It works, but man is it some ugly coding.

Updating any unlocked text box in the form calls the "Update" sub. All it does is check to make sure everything is filled out prior to recalculating.

Code:
Private Sub Update()

If Trim(weeks.Value & vbNullString) <> vbNullString Then
If Trim(days.Value & vbNullString) <> vbNullString Then
If Trim(shifts.Value & vbNullString) <> vbNullString Then
If Trim(hours.Value & vbNullString) <> vbNullString Then
If Trim(volume.Value & vbNullString) <> vbNullString Then
If IsNumeric(weeks.Value) = True Then
If IsNumeric(days.Value) = True Then
If IsNumeric(shifts.Value) = True Then
If IsNumeric(hours.Value) = True Then
If IsNumeric(volume.Value) = True Then
    VPW.Value = Round(volume.Value / weeks.Value, 1)
    VPD.Value = Round(VPW.Value / days.Value, 1)
    VPS.Value = Round(VPD.Value / shifts.Value, 1)
    VPH.Value = Round(VPS.Value / hours.Value, 1)
    E100.Value = (3600 / volume.Value) * weeks.Value * days.Value * shifts.Value * hours.Value
    E95.Value = E100.Value * 0.95
    E90.Value = E100.Value * 0.9
    E85.Value = E100.Value * 0.85
    E80.Value = E100.Value * 0.8
    E75.Value = E100.Value * 0.75
    If ECustomInput.Value = "" Then
        ECustom.Value = 0
        Else
        ECustom.Value = E100.Value * (ECustomInput.Value / 100)
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If

End Sub
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could use AND/OR to restrict the number of tests:

Code:
Private Sub Update()

' All values must be present
If Trim(weeks.Value) = vbNullString Or Trim(Days.Value) = vbNullString Or Trim(shifts.Value) = vbNullString _
Or Trim(hours.Value) = vbNullString Or Trim(volume.Value) = vbNullString Then
    Exit Sub
End If

' All values must be numeric
If IsNumeric(weeks.Value) And IsNumeric(Days.Value) And IsNumeric(shifts.Value) _
And IsNumeric(hours.Value) And IsNumeric(volume.Value) Then
    VPW.Value = Round(volume.Value / weeks.Value, 1)
    VPD.Value = Round(VPW.Value / Days.Value, 1)
    VPS.Value = Round(VPD.Value / shifts.Value, 1)
    VPH.Value = Round(VPS.Value / hours.Value, 1)
    E100.Value = (3600 / volume.Value) * weeks.Value * Days.Value * shifts.Value * hours.Value
    E95.Value = E100.Value * 0.95
    E90.Value = E100.Value * 0.9
    E85.Value = E100.Value * 0.85
    E80.Value = E100.Value * 0.8
    E75.Value = E100.Value * 0.75
    If ECustomInput.Value = "" Then
        ECustom.Value = 0
        Else
        ECustom.Value = E100.Value * (ECustomInput.Value / 100)
    End If
End If

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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