HELP: Adding multiple textbox values (cleanup)

Photomommie

New Member
Joined
Feb 10, 2019
Messages
2
I have 20 textboxes in my userform that need to be added as a "Subtotal"
I have it working now, but it is very clunky and I thought there might be a better way of handling this.

Here is an example of the code:
Code:
Private Sub tbxPrice1_Change()
Dim tbxPriceSum As Double
If Me.tbxPrice1.value = Empty Then Me.tbxPrice1.value = "$0.00"
If Me.tbxPrice2.value = Empty Then Me.tbxPrice2.value = "$0.00"
If Me.tbxPrice3.value = Empty Then Me.tbxPrice3.value = "$0.00"
If Me.tbxPrice4.value = Empty Then Me.tbxPrice4.value = "$0.00"
If Me.tbxPrice5.value = Empty Then Me.tbxPrice5.value = "$0.00"
If Me.tbxPrice6.value = Empty Then Me.tbxPrice6.value = "$0.00"
If Me.tbxPrice7.value = Empty Then Me.tbxPrice7.value = "$0.00"
If Me.tbxPrice8.value = Empty Then Me.tbxPrice8.value = "$0.00"
If Me.tbxPrice9.value = Empty Then Me.tbxPrice9.value = "$0.00"
If Me.tbxPrice10.value = Empty Then Me.tbxPrice10.value = "$0.00"
If Me.tbxPrice11.value = Empty Then Me.tbxPrice11.value = "$0.00"
If Me.tbxPrice12.value = Empty Then Me.tbxPrice12.value = "$0.00"
If Me.tbxPrice13.value = Empty Then Me.tbxPrice13.value = "$0.00"
If Me.tbxPrice14.value = Empty Then Me.tbxPrice14.value = "$0.00"
If Me.tbxPrice15.value = Empty Then Me.tbxPrice15.value = "$0.00"
If Me.tbxPrice16.value = Empty Then Me.tbxPrice16.value = "$0.00"
If Me.tbxPrice17.value = Empty Then Me.tbxPrice17.value = "$0.00"
If Me.tbxPrice18.value = Empty Then Me.tbxPrice18.value = "$0.00"
If Me.tbxPrice19.value = Empty Then Me.tbxPrice19.value = "$0.00"
If Me.tbxPrice20.value = Empty Then Me.tbxPrice20.value = "$0.00"
    Me.tbxPriceSum.value = WorksheetFunction.Sum(Me.tbxPrice1.value, Me.tbxPrice2.value, Me.tbxPrice3.value, Me.tbxPrice4.value, Me.tbxPrice5.value, Me.tbxPrice6.value, Me.tbxPrice7.value, Me.tbxPrice8.value, Me.tbxPrice9.value, Me.tbxPrice10.value, Me.tbxPrice11.value, Me.tbxPrice12.value, Me.tbxPrice13.value, Me.tbxPrice14.value, Me.tbxPrice15.value, Me.tbxPrice16.value, Me.tbxPrice17.value, Me.tbxPrice18.value, Me.tbxPrice19.value, Me.tbxPrice20.value)
    Me.tbxPriceSum.value = Format(Me.tbxPriceSum.value, "$###,##0.00")
End Sub
Private Sub tbxPrice2_Change()
Dim tbxPriceSum As Double
If Me.tbxPrice1.value = Empty Then Me.tbxPrice1.value = "$0.00"
If Me.tbxPrice2.value = Empty Then Me.tbxPrice2.value = "$0.00"
If Me.tbxPrice3.value = Empty Then Me.tbxPrice3.value = "$0.00"
If Me.tbxPrice4.value = Empty Then Me.tbxPrice4.value = "$0.00"
If Me.tbxPrice5.value = Empty Then Me.tbxPrice5.value = "$0.00"
If Me.tbxPrice6.value = Empty Then Me.tbxPrice6.value = "$0.00"
If Me.tbxPrice7.value = Empty Then Me.tbxPrice7.value = "$0.00"
If Me.tbxPrice8.value = Empty Then Me.tbxPrice8.value = "$0.00"
If Me.tbxPrice9.value = Empty Then Me.tbxPrice9.value = "$0.00"
If Me.tbxPrice10.value = Empty Then Me.tbxPrice10.value = "$0.00"
If Me.tbxPrice11.value = Empty Then Me.tbxPrice11.value = "$0.00"
If Me.tbxPrice12.value = Empty Then Me.tbxPrice12.value = "$0.00"
If Me.tbxPrice13.value = Empty Then Me.tbxPrice13.value = "$0.00"
If Me.tbxPrice14.value = Empty Then Me.tbxPrice14.value = "$0.00"
If Me.tbxPrice15.value = Empty Then Me.tbxPrice15.value = "$0.00"
If Me.tbxPrice16.value = Empty Then Me.tbxPrice16.value = "$0.00"
If Me.tbxPrice17.value = Empty Then Me.tbxPrice17.value = "$0.00"
If Me.tbxPrice18.value = Empty Then Me.tbxPrice18.value = "$0.00"
If Me.tbxPrice19.value = Empty Then Me.tbxPrice19.value = "$0.00"
If Me.tbxPrice20.value = Empty Then Me.tbxPrice20.value = "$0.00"
    Me.tbxPriceSum.value = WorksheetFunction.Sum(Me.tbxPrice1.value, Me.tbxPrice2.value, Me.tbxPrice3.value, Me.tbxPrice4.value, Me.tbxPrice5.value, Me.tbxPrice6.value, Me.tbxPrice7.value, Me.tbxPrice8.value, Me.tbxPrice9.value, Me.tbxPrice10.value, Me.tbxPrice11.value, Me.tbxPrice12.value, Me.tbxPrice13.value, Me.tbxPrice14.value, Me.tbxPrice15.value, Me.tbxPrice16.value, Me.tbxPrice17.value, Me.tbxPrice18.value, Me.tbxPrice19.value, Me.tbxPrice20.value)
    Me.tbxPriceSum.value = Format(Me.tbxPriceSum.value, "$###,##0.00")
End Sub

Etc.... for 20 textboxes.
Is there a cleaner way to do this???
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Not sure why you think you need all this code clearing the 20 Textboxes.

Here is a loop that will work for 6 of the Textboxes.

Change 6 to 20 for your use.

This script will sum the first 6 Textboxes.

Code:
Private Sub CommandButton2_Click()
'Modified  5/9/2019  12:25:50 AM  EDT
Dim i As Long
Dim ans As Long
For i = 1 To 6
    ans = CDbl(Controls("tbxPrice" & i).Value) + ans
Next
tbxPriceSum.Value = ans
End Sub
 
Upvote 0
If you want to clear all your UserForm TextBoxes.
Try this script:

Code:
Private Sub CommandButton3_Click()
'Modified  5/9/2019  12:41:03 AM  EDT
    
    For Each Control In Me.Controls
        If TypeName(Control) = "TextBox" Then Control.Value = ""
    Next
End Sub
 
Upvote 0
Or maybe you want this:

Code:
Private Sub CommandButton3_Click()
'Modified  5/9/2019  12:45:03 AM  EDT
    
    For Each Control In Me.Controls
        If TypeName(Control) = "TextBox" Then Control.Value = "$0.00"
    Next
End Sub
 
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