Auto-Update Userform Label when text boxes change values

tryingmybest418

New Member
Joined
Jan 22, 2018
Messages
32
Hi all,

I have a userform with 18 textboxes, all set up to only accept numbers.

I have a label that is set up to display the sum of the 18 textboxes, and have the textboxes and label set up to read any empty text boxes as "0".

I am looking for a way to have the label automatically update when any of the text box values changes. Is this doable?

Thanks!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,
what you want should be doable

One way would be to create a common code for each of your textbox change events to call


Place in your forms code page:

Rich (BB code):
Private Sub SumTextBoxes()
        Dim Ctl As Control
        Dim Total As Double
        For Each Ctl In Me.Controls
              If TypeName(Ctl) = "TextBox" Then Total = Total + Val(Ctl.Value)
        Next Ctl
        
        Me.Label1.Caption = Total
End Sub

Change your forms Label name shown in RED as required.

Each of your TextBoxes will require a change event to call the procedure


Rich (BB code):
Private Sub TextBox1_Change()
    SumTextBoxes
End Sub


Private Sub TextBox2_Change()
    SumTextBoxes
End Sub


Private Sub TextBox3_Change()
    SumTextBoxes
End Sub


Private Sub TextBox4_Change()
    SumTextBoxes
End Sub

'etc
'etc

a less busy way would be to create a Class for the Textboxes which would then only require one change event. But see how you get on with above suggestion first.


Dave
 
Last edited:
Upvote 0
This is brilliant, thanks so much. It's working grat.

Now I do have two other text boxes on the form that are also numbers, but I don't want to include them in the sum, would there be a way to do that?

I've named those "txtSlopeRating" and "txtCourseRating".
While the text boxes I want to sum are all labeled "txtParHole1", "txtParHole2" ...etc. If there's a way to reference that?

Thanks again!
 
Upvote 0
Hi,
try this update

Code:
Private Sub SumTextBoxes()
    Dim Total As Double
    Dim i As Integer
    For i = 1 To 18
        Total = Total + Val(Me.Controls("txtParHole" & i).Value)
    Next i
    Me.Label1.Caption = Total
End Sub

This assumes that your textboxes are named "txtParHole1", "txtParHole2" through to "txtParHole18"

Dave
 
Upvote 0
Oh, you mentioned using a "class" that would not require 18 different change events. How would that work for this?

Hi, Try this.

1 – make a backup of your workbook

2 – In your Usefrorm, DELETE ALL the change events & the SumTextBoxes codes

2-1 Place following in your Userforms code page

Rich (BB code):
 Private Sub UserForm_Initialize()    
    Dim i As Integer
    ReDim TextBox(1 To 18)
    For i = 1 To 18
        Set TextBox(i).TextBoxClass = Me.Controls("txtParHole" & i)
    Next
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Set TextBoxClass = Nothing
End Sub

3 – From VBA editor Menu Insert >Module

  • Place all following code

Rich (BB code):
 Public TextBox() As New TextBoxClass

Sub SumTextBoxes(ByVal Form As Object)
    Dim Total As Double
    Dim Box As Variant
    For Each Box In TextBox
        Total = Total + Val(Box.TextBoxClass.Value)
    Next
    Form.Label1.Caption = Total
End Sub

Change only the Label name shown in RED as required.
Note the Public Declaration which MUST sit at the very TOP of the Module OUTSIDE any procedure.

4 – From VBA editor Menu Insert > Class Module

  • Name the Module TextBoxClass << Very Important that you do this


  • Place all following code

Rich (BB code):
 Public WithEvents TextBoxClass As MSForms.TextBox

Private Sub TextBoxClass_Change()
    SumTextBoxes TextBoxClass.Parent
End Sub

Note the Public Declaration which MUST sit at the very TOP of the Class Module OUTSIDE any procedure.

Close the editor & run your form, which should sum the entered values.
Just a mention you can if needed, add a single key press event in the class to call a function to limit numeric values only being entered.

Hopefully, all will work as required but I am not around much this week - if you have issues, you can always revert to your backup copy.

Dave
 
Last edited:
Upvote 0
Sorry for the delay - I've been away and haven't revisited this.

Thank you so much for the information. I'll look through this and try it out.

I appreciate all the help!
 
Upvote 0

Forum statistics

Threads
1,215,181
Messages
6,123,508
Members
449,101
Latest member
mgro123

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