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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

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 :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,031
Messages
5,856,937
Members
431,841
Latest member
jaybeem

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
Top