Problem adding userform textboxes in vba

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46
Hi can someone help me out on this piece of code that is giving me an error.


Code:
Dim xtotal As Integer
 
xtotal = Int(TextBox11.Value) + Int(TextBox12.Value) + Int(TextBox13.Value) + _
Int(TextBox14.Value) + Int(TextBox15.Value) + Int(TextBox16.Value) + _
Int(TextBox17.Value) + Int(TextBox18.Value) + Int(TextBox19.Value) + _
Int(TextBox20.Value)
       
 
If xtotal = Int(TextBox21.Value) Then

Thanks :)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You are using the wrong function.

The INT function rounds a value to the nearest integer.
E.g.,
Int(99.6) would round up to 100
Int(99.1) would round down to 99

TextBoxes return a value of data type string. So you will need to convert the value to an integer before using it in a calculation
e.g.,
xTotal = CInt(TextBox1.value)+CInt(Texttbox2.value) etc.

Try this:
Highligh your formula
Click Edit => Replace
Replace Int with CInt
 
Last edited:
Upvote 0
thx for your quick response :)

im still getting an error after converting because some of the textboxes are blank "" . i pretend add all 10 textboxes with or without values in them example.

textbox1 = 100 textbox2 =100 textbox3="" result would be 200

is there a way i can do this... because the user will not always be inputting values in all the textboxes.

Thanks for your help
 
Upvote 0
Try using the VAL function

xTotal = Val(TextBox1.Value) + Val(TextBox2.Value)
 
Upvote 0
lemme paste the some more of the code so u can have a better look at it

Code:
Private Sub CommandButton1_Click()

xtotal = Val(TextBox11.Value) + Val(TextBox12.Value) + Val(TextBox13.Value) + _
Val(TextBox14.Value) + Val(TextBox15.Value) + Val(TextBox16.Value) + _
Val(TextBox17.Value) + Val(TextBox18.Value) + Val(TextBox19.Value) + _
Val(TextBox20.Value)

       If xtotal = Val(TextBox21.Value) Then
       
       
       
       
       



If TextBox23.Value = "2" Then

If TextBox1.Value = "" Or TextBox11.Value = "" Or TextBox24.Value = "" Or TextBox2.Value = "" Or TextBox12.Value = "" Or TextBox25.Value = "" Then

MsgBox "preencha todos os dados"

TextBox1.SetFocus

Exit Sub
End If
End If


If TextBox23.Value = "3" Then

If TextBox1.Value = "" Or TextBox11.Value = "" Or TextBox24.Value = "" Or _
TextBox2.Value = "" Or TextBox12.Value = "" Or TextBox25.Value = "" Or _
TextBox3.Value = "" Or TextBox13.Value = "" Or TextBox26.Value = "" Then

MsgBox "preencha todos os dados"

TextBox1.SetFocus

Exit Sub
End If
End If


etc etc
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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