VBA Express Forum  




Go Back   VBA Express Forum > VBA Code & Other Help > Excel Help
     Feedback     
Register FAQ Members Arcade Knowledge Base Training Articles Consulting

Reply
 
Thread Tools Display Modes
Old 03-05-2007, 02:20 AM   #1
lifeson

 
Joined: Dec 2006
Posts: 271
Kb Entries: 0
Articles: 0
Sum text box values

This seems so simple I'm almost embarressed to ask but:
I have a form where I want a text box to display the total of the textbox values on the form.

VBA:
Private Sub Calculate() On Error Resume Next TextBox3.Value = TextBox1.Value + TextBox2.Value ' TextBox3.Value = (Format(TextBox, "0.00")) End Sub Private Sub TextBox1_Change() OnlyNumbers Calculate End Sub Private Sub TextBox2_Change() OnlyNumbers Calculate End Sub Private Sub OnlyNumbers() If TypeName(Me.ActiveControl) = "TextBox" Then With Me.ActiveControl If Not IsNumeric(.Value) And .Value <> vbNullString Then MsgBox "Sorry, only numbers allowed" .Value = vbNullString End If End With End If End Sub
VBA tags courtesy of www.thecodenet.com
This works when multiplying the values
VBA:
TextBox3.Value = TextBox1.Value * TextBox2.Value
VBA tags courtesy of www.thecodenet.com
So i must be in the right area???

As an add on I would like the total to be rounded to be the sum of textbox1 and textbox2, divided by 1000 and dsiplayed to 2 figures

eg

1234 + 3456 = 4.69

Local Time: 07:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Old 03-05-2007, 02:38 AM   #2
xld
 
xld's Avatar
Distinguished Lord of VBAX

 
Joined: Apr 2005
Posts: 23,118
Kb Entries: 3
Articles: 2
VBA:
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
VBA tags courtesy of www.thecodenet.com

Local Time: 08:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Old 03-05-2007, 03:04 AM   #3
lifeson

 
Joined: Dec 2006
Posts: 271
Kb Entries: 0
Articles: 0
sub or function not defined

XLD
If I just copy your code:
VBA:
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
VBA tags courtesy of www.thecodenet.com

over my line
VBA:
TextBox3.Value = TextBox1.Value + TextBox2.Value
VBA tags courtesy of www.thecodenet.com
I get an error:

"Sub or function not defined."

Local Time: 07:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Old 03-05-2007, 03:12 AM   #4
moa
 
moa's Avatar

 
Joined: Nov 2006
Posts: 177
Kb Entries: 0
Articles: 0
Look at the first CDbl function call (two "l"s). It's a typo.


Glen

Local Time: 08:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Old 03-05-2007, 03:23 AM   #5
xld
 
xld's Avatar
Distinguished Lord of VBAX

 
Joined: Apr 2005
Posts: 23,118
Kb Entries: 3
Articles: 2
Quote:
 
Originally Posted by: lifeson
XLD
If I just copy your code:
VBA:
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
VBA tags courtesy of www.thecodenet.com

over my line
VBA:
TextBox3.Value = TextBox1.Value + TextBox2.Value
VBA tags courtesy of www.thecodenet.com
I get an error:

"Sub or function not defined."


Sorry, I changed the Val to CDbl just before posting (Because of an issue with another post a few days ago), but messed it. It should be

VBA:
TextBox3.Value = Format((CDbl(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
VBA tags courtesy of www.thecodenet.com

Local Time: 08:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Old 03-05-2007, 08:28 AM   #6
lifeson

 
Joined: Dec 2006
Posts: 271
Kb Entries: 0
Articles: 0
Cheers
As usual thanx for the help

Local Time: 07:38 AM
Local Date: 05-20-2013
Location:

 
Reply With Quote Top
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 12:38 AM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © 2004 - 2012 VBA Express