Sum of TextBoxes

harveya915

Board Regular
Joined
Sep 4, 2015
Messages
141
I have a UserForm with 3 TextBoxes. I have a code (inside a Module2) that will find the sum of the contents of TextBox3 + TextBox4 = TextBox5. The code looks like this:

VBA Code:
Sub SUM_CAL()
Dim X As Double
X = 0
If Len(UserForm1.TextBox3.Value) > 0 Then X = X + UserForm1.TextBox3.Value
If Len(UserForm1.TextBox4.Value) > 0 Then X = X + UserForm1.TextBox4.Value
UserForm1.TextBox5 = X

Then in the UserForm code I have:

VBA Code:
Private Sub TextBox3_Change()
Call Module2.SUM_CAL
End Sub

VBA Code:
Private Sub TextBox4_Change()
Call Module2.SUM_CAL
End Sub

Recently I made some changes to the UserForm. I added a CheckBox1 that when the value is True it adds 2 more TextBoxes to the UserForm (TextBox10 and TextBox11)

I need for when these new TextBoxes become visible for the following calculations:
TextBox3 + TextBox10 = TextBox11
Then
TextBox11 + TextBox4 = TextBox5

But if the CheckBox1 remains unchecked/False for the original equation to remain: TextBox3 + TextBox4 = TextBox5
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
So I tinkered with it a bit and I think I just solved my own problem. I just repeated my original code but substituted the X for Y and then again for Z. First Figuring the sum for TextBox3 + TextBox10 = 11 (Y) and then figuring out TextBox11 + TextBox4 = TextBox5 (Z) and it goes a little something like this.

VBA Code:
Sub SUM_CAL()

Dim X As Double
X = 0
If Len(CPF_Pay_CallInEPayCC.TextBox3.Value) > 0 Then X = X + CPF_Pay_CallInEPayCC.TextBox3.Value
If Len(CPF_Pay_CallInEPayCC.TextBox4.Value) > 0 Then X = X + CPF_Pay_CallInEPayCC.TextBox4.Value
CPF_Pay_CallInEPayCC.TextBox5 = X

Dim Y As Double
Y = 0
If Len(CPF_Pay_CallInEPayCC.TextBox3.Value) > 0 Then Y = Y + CPF_Pay_CallInEPayCC.TextBox3.Value
If Len(CPF_Pay_CallInEPayCC.TextBox10.Value) > 0 Then Y = Y + CPF_Pay_CallInEPayCC.TextBox10.Value
CPF_Pay_CallInEPayCC.TextBox11 = Y

Dim Z As Double
Z = 0
If Len(CPF_Pay_CallInEPayCC.TextBox11.Value) > 0 Then Z = Z + CPF_Pay_CallInEPayCC.TextBox11.Value
If Len(CPF_Pay_CallInEPayCC.TextBox4.Value) > 0 Then Z = Z + CPF_Pay_CallInEPayCC.TextBox4.Value
CPF_Pay_CallInEPayCC.TextBox5 = Z

End Sub

Right now the problem that I have is that if the CheckBox remains unchecked/False, then the X code works. Then when I do check/True the CheckBox1 then the whole code kicks in. But, if I uncheck/False again the CheckBox1 then the code remains active as if it was still checked. The only work around I have found is to check/True the checkbox once again and zero out TextBox10.

I would appreciate any feedback and suggestions with this. Much thanks!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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