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 :)
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

bertie

Well-known Member
Joined
Jun 12, 2009
Messages
1,869
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:

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46
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
 

bertie

Well-known Member
Joined
Jun 12, 2009
Messages
1,869
Try using the VAL function

xTotal = Val(TextBox1.Value) + Val(TextBox2.Value)
 

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46

ADVERTISEMENT

now i get a runtime error 6... overflow :s ouchy :)
 

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46
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
 

chichaspowa

New Member
Joined
Sep 6, 2010
Messages
46
oki think i got it bertie.... changed the integer variable to long...


thanks for your great help :)
 

Watch MrExcel Video

Forum statistics

Threads
1,108,960
Messages
5,525,914
Members
409,671
Latest member
nasseralateek

This Week's Hot Topics

Top