HiUserForm.png
I have a UserForm (pic attached) that allows the user to input sales for up to 5 years for each month.
They can do this by entering a 'master' percentage at the top which calculates the following years sales based on the previous or they can enter everything manually.
Also, entering a 'master' percentage doesn't stop the user from changing a percentage on one particular month, so it's pretty much 100% able to do whatever the user wants.
It also has totals.
The code works perfect BUT it needs to be applied to 10's of other UserForms which form part of a big workbook.
So, can any of this code be shortened because at the moment when I create a new UserForm I have to change every bit of code ever so slightly to make sure it works with the new TextBox names?
Submits the data onto the worksheet:
The second submit button just opens the next UserForm.Private Sub cmdSY1_5S1C_Click() Dim ws As Worksheet: Set ws = Worksheets("MASTER") Dim i As Integer 'Year 1: For i = 1 To 12 If Me.Controls("txtY1M" & i).Value <> "" Then ws.Cells(17, 19 + i).Value = Me.Controls("txtY1M" & i).Value End If Next i 'Year 2: For i = 1 To 12 If Me.Controls("txtY2M" & i).Value <> "" Then ws.Cells(17, 31 + i).Value = Me.Controls("txtY2M" & i).Value End If Next i 'Year 3: For i = 1 To 12 If Me.Controls("txtY3M" & i).Value <> "" Then ws.Cells(17, 43 + i).Value = Me.Controls("txtY3M" & i).Value End If Next i 'Year 4: For i = 1 To 12 If Me.Controls("txtY3M" & i).Value <> "" Then ws.Cells(17, 55 + i).Value = Me.Controls("txtY3M" & i).Value End If Next i 'Year 5: For i = 1 To 12 If Me.Controls("txtY3M" & i).Value <> "" Then ws.Cells(17, 67 + i).Value = Me.Controls("txtY3M" & i).Value End If Next i frmY1_5VATSalesType1.Hide End Sub
Makes all the individual percentage boxes equal the master above it:
Upon exit of entering an individual percentage, the following years sales will re-calculate (very long as it works for 60 months):Private Sub txtIncDecST1M1_Change() If txtIncDecST1M1.Value > "" Then txtIncDec1.Value = txtIncDecST1M1.Value txtIncDec2.Value = txtIncDecST1M1.Value txtIncDec3.Value = txtIncDecST1M1.Value txtIncDec4.Value = txtIncDecST1M1.Value txtIncDec5.Value = txtIncDecST1M1.Value txtIncDec6.Value = txtIncDecST1M1.Value txtIncDec7.Value = txtIncDecST1M1.Value txtIncDec8.Value = txtIncDecST1M1.Value txtIncDec9.Value = txtIncDecST1M1.Value txtIncDec10.Value = txtIncDecST1M1.Value txtIncDec11.Value = txtIncDecST1M1.Value txtIncDec12.Value = txtIncDecST1M1.Value End If End Sub Private Sub txtIncDecST1M2_Change() If txtIncDecST1M2.Value > "" Then txtIncDec13.Value = txtIncDecST1M2.Value txtIncDec14.Value = txtIncDecST1M2.Value txtIncDec15.Value = txtIncDecST1M2.Value txtIncDec16.Value = txtIncDecST1M2.Value txtIncDec17.Value = txtIncDecST1M2.Value txtIncDec18.Value = txtIncDecST1M2.Value txtIncDec19.Value = txtIncDecST1M2.Value txtIncDec20.Value = txtIncDecST1M2.Value txtIncDec21.Value = txtIncDecST1M2.Value txtIncDec22.Value = txtIncDecST1M2.Value txtIncDec23.Value = txtIncDecST1M2.Value txtIncDec24.Value = txtIncDecST1M2.Value End If End Sub Private Sub txtIncDecST1M3_Change() If txtIncDecST1M3.Value > "" Then txtIncDec25.Value = txtIncDecST1M3.Value txtIncDec26.Value = txtIncDecST1M3.Value txtIncDec27.Value = txtIncDecST1M3.Value txtIncDec28.Value = txtIncDecST1M3.Value txtIncDec29.Value = txtIncDecST1M3.Value txtIncDec30.Value = txtIncDecST1M3.Value txtIncDec31.Value = txtIncDecST1M3.Value txtIncDec32.Value = txtIncDecST1M3.Value txtIncDec33.Value = txtIncDecST1M3.Value txtIncDec34.Value = txtIncDecST1M3.Value txtIncDec35.Value = txtIncDecST1M3.Value txtIncDec36.Value = txtIncDecST1M3.Value End If End Sub Private Sub txtIncDecST1M4_Change() If txtIncDecST1M4.Value > "" Then txtIncDec37.Value = txtIncDecST1M4.Value txtIncDec38.Value = txtIncDecST1M4.Value txtIncDec39.Value = txtIncDecST1M4.Value txtIncDec40.Value = txtIncDecST1M4.Value txtIncDec41.Value = txtIncDecST1M4.Value txtIncDec42.Value = txtIncDecST1M4.Value txtIncDec43.Value = txtIncDecST1M4.Value txtIncDec44.Value = txtIncDecST1M4.Value txtIncDec45.Value = txtIncDecST1M4.Value txtIncDec46.Value = txtIncDecST1M4.Value txtIncDec47.Value = txtIncDecST1M4.Value txtIncDec48.Value = txtIncDecST1M4.Value End If End Sub
Private Sub txtIncDec1_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec1.Value <> "" Then txtY2M1 = txtY1M1 * (1 + Val(txtIncDec1 / 100)) End If End Sub Private Sub txtIncDec2_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec2.Value <> "" Then txtY2M2 = txtY1M2 * (1 + Val(txtIncDec2 / 100)) End If End Sub Private Sub txtIncDec3_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec3.Value <> "" Then txtY2M3 = txtY1M3 * (1 + Val(txtIncDec3 / 100)) End If End Sub Private Sub txtIncDec4_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec4.Value <> "" Then txtY2M4 = txtY1M4 * (1 + Val(txtIncDec4 / 100)) End If End Sub Private Sub txtIncDec5_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec5.Value <> "" Then txtY2M5 = txtY1M5 * (1 + Val(txtIncDec5 / 100)) End If End Sub Private Sub txtIncDec6_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec6.Value <> "" Then txtY2M6 = txtY1M6 * (1 + Val(txtIncDec6 / 100)) End If End Sub Private Sub txtIncDec7_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec7.Value <> "" Then txtY2M7 = txtY1M7 * (1 + Val(txtIncDec7 / 100)) End If End Sub Private Sub txtIncDec8_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec8.Value <> "" Then txtY2M8 = txtY1M8 * (1 + Val(txtIncDec8 / 100)) End If End Sub Private Sub txtIncDec9_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec9.Value <> "" Then txtY2M9 = txtY1M9 * (1 + Val(txtIncDec9 / 100)) End If End Sub Private Sub txtIncDec10_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec10.Value <> "" Then txtY2M10 = txtY1M10 * (1 + Val(txtIncDec10 / 100)) End If End Sub Private Sub txtIncDec11_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec11.Value <> "" Then txtY2M11 = txtY1M11 * (1 + Val(txtIncDec11 / 100)) End If End Sub Private Sub txtIncDec12_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec12.Value <> "" Then txtY2M12 = txtY1M12 * (1 + Val(txtIncDec12 / 100)) End If End Sub Private Sub txtIncDec13_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec13.Value <> "" Then txtY3M1 = txtY2M1 * (1 + Val(txtIncDec13 / 100)) End If End Sub Private Sub txtIncDec14_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec14.Value <> "" Then txtY3M2 = txtY2M2 * (1 + Val(txtIncDec14 / 100)) End If End Sub Private Sub txtIncDec15_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec15.Value <> "" Then txtY3M3 = txtY2M3 * (1 + Val(txtIncDec15 / 100)) End If End Sub Private Sub txtIncDec16_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec16.Value <> "" Then txtY3M4 = txtY2M4 * (1 + Val(txtIncDec16 / 100)) End If End Sub Private Sub txtIncDec17_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec17.Value <> "" Then txtY3M5 = txtY2M5 * (1 + Val(txtIncDec17 / 100)) End If End Sub Private Sub txtIncDec18_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec18.Value <> "" Then txtY3M6 = txtY2M6 * (1 + Val(txtIncDec18 / 100)) End If End Sub Private Sub txtIncDec19_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec19.Value <> "" Then txtY3M7 = txtY2M7 * (1 + Val(txtIncDec19 / 100)) End If End Sub Private Sub txtIncDec20_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec20.Value <> "" Then txtY3M8 = txtY2M8 * (1 + Val(txtIncDec20 / 100)) End If End Sub Private Sub txtIncDec21_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec21.Value <> "" Then txtY3M9 = txtY2M9 * (1 + Val(txtIncDec21 / 100)) End If End Sub Private Sub txtIncDec22_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec22.Value <> "" Then txtY3M10 = txtY2M10 * (1 + Val(txtIncDec22 / 100)) End If End Sub Private Sub txtIncDec23_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec23.Value <> "" Then txtY3M11 = txtY2M11 * (1 + Val(txtIncDec23 / 100)) End If End Sub Private Sub txtIncDec24_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec24.Value <> "" Then txtY3M12 = txtY2M12 * (1 + Val(txtIncDec24 / 100)) End If End Sub Private Sub txtIncDec25_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec25.Value <> "" Then txtY4M1 = txtY3M1 * (1 + Val(txtIncDec25 / 100)) End If End Sub Private Sub txtIncDec26_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec26.Value <> "" Then txtY4M2 = txtY3M2 * (1 + Val(txtIncDec26 / 100)) End If End Sub Private Sub txtIncDec27_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec27.Value <> "" Then txtY4M3 = txtY3M3 * (1 + Val(txtIncDec27 / 100)) End If End Sub Private Sub txtIncDec28_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec28.Value <> "" Then txtY4M4 = txtY3M4 * (1 + Val(txtIncDec28 / 100)) End If End Sub Private Sub txtIncDec29_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec29.Value <> "" Then txtY4M5 = txtY3M5 * (1 + Val(txtIncDec29 / 100)) End If End Sub Private Sub txtIncDec30_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec30.Value <> "" Then txtY4M6 = txtY3M6 * (1 + Val(txtIncDec30 / 100)) End If End Sub Private Sub txtIncDec31_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec31.Value <> "" Then txtY4M7 = txtY3M7 * (1 + Val(txtIncDec31 / 100)) End If End Sub Private Sub txtIncDec32_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec32.Value <> "" Then txtY4M8 = txtY3M8 * (1 + Val(txtIncDec32 / 100)) End If End Sub Private Sub txtIncDec33_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec33.Value <> "" Then txtY4M9 = txtY3M9 * (1 + Val(txtIncDec33 / 100)) End If End Sub Private Sub txtIncDec34_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec34.Value <> "" Then txtY4M10 = txtY3M10 * (1 + Val(txtIncDec34 / 100)) End If End Sub Private Sub txtIncDec35_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec35.Value <> "" Then txtY4M11 = txtY3M11 * (1 + Val(txtIncDec35 / 100)) End If End Sub Private Sub txtIncDec36_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec36.Value <> "" Then txtY4M12 = txtY3M12 * (1 + Val(txtIncDec36 / 100)) End If End Sub Private Sub txtIncDec37_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec37.Value <> "" Then txtY5M1 = txtY4M1 * (1 + Val(txtIncDec37 / 100)) End If End Sub Private Sub txtIncDec38_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec38.Value <> "" Then txtY5M2 = txtY4M2 * (1 + Val(txtIncDec38 / 100)) End If End Sub Private Sub txtIncDec39_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec39.Value <> "" Then txtY5M3 = txtY4M3 * (1 + Val(txtIncDec39 / 100)) End If End Sub Private Sub txtIncDec40_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec40.Value <> "" Then txtY5M4 = txtY4M4 * (1 + Val(txtIncDec40 / 100)) End If End Sub Private Sub txtIncDec41_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec41.Value <> "" Then txtY5M5 = txtY4M5 * (1 + Val(txtIncDec41 / 100)) End If End Sub Private Sub txtIncDec42_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec42.Value <> "" Then txtY5M6 = txtY4M6 * (1 + Val(txtIncDec42 / 100)) End If End Sub Private Sub txtIncDec43_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec43.Value <> "" Then txtY5M7 = txtY4M7 * (1 + Val(txtIncDec43 / 100)) End If End Sub Private Sub txtIncDec44_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec44.Value <> "" Then txtY5M8 = txtY4M8 * (1 + Val(txtIncDec44 / 100)) End If End Sub Private Sub txtIncDec45_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec45.Value <> "" Then txtY5M9 = txtY4M9 * (1 + Val(txtIncDec45 / 100)) End If End Sub Private Sub txtIncDec46_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec46.Value <> "" Then txtY5M10 = txtY4M10 * (1 + Val(txtIncDec46 / 100)) End If End Sub Private Sub txtIncDec47_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec47.Value <> "" Then txtY5M11 = txtY4M11 * (1 + Val(txtIncDec47 / 100)) End If End Sub Private Sub txtIncDec48_Exit(ByVal Cancel As MSForms.ReturnBoolean) On Error Resume Next If txtIncDec48.Value <> "" Then txtY5M12 = txtY4M12 * (1 + Val(txtIncDec48 / 100)) End If End Sub
Bookmarks