How to sum textbox from a Form?

shyy

Well-known Member
Joined
Nov 6, 2008
Messages
1,484
Hey guys,

I am having trouble with my form. Everything works correctly as long as there is a number value. If a textbox is blank, then I would get an error. Is there any way to get around this? I don't want the user to have to input the value of 0 in order to sum all the textboxes.

Thanks in advance

This is the code I used

Code:
Private Sub TextBox44_Change()
Me.TextBox44.Value = CDbl(Line1.Value) + CDbl(Line2.Value) + CDbl(Line3.Value) + CDbl(Line4.Value) + CDbl(Line5.Value) + CDbl(Line6a.Value) + CDbl(Line6b.Value) + CDbl(Line7.Value) + CDbl(Line8.Value) + CDbl(Line9a.Value) + CDbl(Line9b.Value) + CDbl(Line9c.Value) + CDbl(Line10.Value) + CDbl(Line11.Value) + CDbl(Line12.Value) + CDbl(Line13.Value) + CDbl(Line14.Value) + CDbl(Line15.Value) + CDbl(Line16.Value) + CDbl(Line17.Value) + CDbl(Line18.Value) + CDbl(Line19.Value) + CDbl(Line20.Value)
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello shyy,

Use the Val function instead of CDbl. It will convert the string to a double number. However, when it reaches a character it can not convert it will stop converting and no error is generated. An empty TextBox will return a value of zero.
Code:
Private Sub TextBox44_Change()
Me.TextBox44.Value = Val(Line1.Value) + Val(Line2.Value) + Val(Line3.Value) _
                   + Val(Line4.Value) + Val(Line5.Value) + Val(Line6a.Value) _
                   + Val(Line6b.Value) + Val(Line7.Value) + Val(Line8.Value) _
                   + Val(Line9a.Value) + Val(Line9b.Value) + Val(Line9c.Value) _
                   + Val(Line10.Value) + Val(Line11.Value) + Val(Line12.Value) _
                   + Val(Line13.Value) + Val(Line14.Value) + Val(Line15.Value) _
                   + Val(Line16.Value) + Val(Line17.Value) + Val(Line18.Value) _
                   + Val(Line19.Value) + Val(Line20.Value)

End Sub
Sincerely,
Leith Ross
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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