Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Greetings,
I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
not tested but try: T10.Text = CDbl(T8.Text) + CDbl(T9.Text) -- Regards Frank Kabel Frankfurt, Germany Minitman wrote: Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hey Frank,
Thanks for the response Both you and Devin came up with similar solutions, and they both work very well, [CDec() and CDbl()], I can not see any advantage of using one over the other, is there one? Thank you. -Minitman On Tue, 14 Dec 2004 22:54:26 +0100, "Frank Kabel" wrote: Hi not tested but try: T10.Text = CDbl(T8.Text) + CDbl(T9.Text) -- Regards Frank Kabel Frankfurt, Germany Minitman wrote: Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
see Tom's reply :-) -- Regards Frank Kabel Frankfurt, Germany Minitman wrote: Hey Frank, Thanks for the response Both you and Devin came up with similar solutions, and they both work very well, [CDec() and CDbl()], I can not see any advantage of using one over the other, is there one? Thank you. -Minitman On Tue, 14 Dec 2004 22:54:26 +0100, "Frank Kabel" wrote: Hi not tested but try: T10.Text = CDbl(T8.Text) + CDbl(T9.Text) -- Regards Frank Kabel Frankfurt, Germany Minitman wrote: Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Convert the text to an numerical data type such as Decimal
for exmpl: Dim totl As Variant totl = CDec(TextBox1.Value) + CDec(TextBox2.Value) HTH Devin -----Original Message----- Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman . |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hey Devin,
The CDec() worked like a charm as did the CDbl()(from Frank's reply). Thank you. -Minitman On Tue, 14 Dec 2004 13:57:49 -0800, "DMoney" wrote: Convert the text to an numerical data type such as Decimal for exmpl: Dim totl As Variant totl = CDec(TextBox1.Value) + CDec(TextBox2.Value) HTH Devin -----Original Message----- Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman . |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Here is why
? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng -- Regards, Tom Ogilvy "Minitman" wrote in message ... Hey Devin, The CDec() worked like a charm as did the CDbl()(from Frank's reply). Thank you. -Minitman On Tue, 14 Dec 2004 13:57:49 -0800, "DMoney" wrote: Convert the text to an numerical data type such as Decimal for exmpl: Dim totl As Variant totl = CDec(TextBox1.Value) + CDec(TextBox2.Value) HTH Devin -----Original Message----- Greetings, I am trying to add two numbers together but I can't get it to give the right answer. Where T8 = $100.00 and T9 = $ 8.00. Both T8 and T9 are TextBoxes as is T10. Here are a couple of the variations of code that I tried: T10.Text = Val(T8.Text) + Val(T9.Text) returns 0 T10.Text = Val(T8.Text) + T9.Text returns 8 T10.Text = T8.Text + Val(T9.Text) returns 100 T10.Text = T8.Text + T9.Text T10.Text = T8.Value + T9.Text T10.Text = T8.Text + T9.Value T10.Text = T8.Value + T9.Value Each returns $ 100.00$ 8.00 Does anyone see what is wrong? Any help would be appreciated. TIA -Minitman . |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks Tom, that clarifies the matter.
-Minitman On Tue, 14 Dec 2004 21:52:03 -0500, "Tom Ogilvy" wrote: Here is why ? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hey Tom,
What if instead of a number you have "" sometimes and 0 in others, in the same TextBox. What do you use then. -Minitman On Tue, 14 Dec 2004 21:52:03 -0500, "Tom Ogilvy" wrote: Here is why ? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Put in checks for those conditions
if tb8.Value = "" then vVal = 0 elseif tb8.Value = 0 then vVal = 0 elseif isnumeric(tb8.value) then vVal = cdbl(tb8.value) else vVal = 0 End if or probably better: if isnumeric(tb8.Value) and tb8.Value < "" then vVal = cdbl(tb8.Value) else vVal = 0 End if -- Regards, Tom Ogilvy "Minitman" wrote in message ... Hey Tom, What if instead of a number you have "" sometimes and 0 in others, in the same TextBox. What do you use then. -Minitman On Tue, 14 Dec 2004 21:52:03 -0500, "Tom Ogilvy" wrote: Here is why ? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hey Tom,
After rereading this I realized that one question remains, if Val and CDbl both have problems with 8.00 %, then what will work? This would seem to apply to my last question. Any help would be appreciated. TIA -Minitman On Tue, 14 Dec 2004 21:52:03 -0500, "Tom Ogilvy" wrote: Here is why ? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Val doesn't have a problem with it - it just doesn't utilize the %. The
solution was to multiply it by .01 -- Regards, Tom Ogilvy "Minitman" wrote in message ... Hey Tom, After rereading this I realized that one question remains, if Val and CDbl both have problems with 8.00 %, then what will work? This would seem to apply to my last question. Any help would be appreciated. TIA -Minitman On Tue, 14 Dec 2004 21:52:03 -0500, "Tom Ogilvy" wrote: Here is why ? val("$8.00") 0 Val stops evaluating at the first non-numeric character. That is why it didn't work "right" with 8.00%. However, cdbl will accept and ignore the $, but will cause an error with "8.00%" Using cdec is overkill. It uses up 14 bytes to store a value and is designed for huge numbers. Double (8 bytes) or single (4 bytes) is more realistic: cdbl or csng |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
This example worked fine for me on a userform. TextBox3.Value = Format(Val(TextBox1.Value) + Val(TextBox2.Value), "$###.00") TextBox1.Value = Format(Val(TextBox1.Value), "$###.00") TextBox2.Value = Format(Val(TextBox2.Value), "$###.00") HTH Ken |
#14
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hey Ken,
Thanks for the reply, however it didn't work for me. As written (Names changes to actual), this code overwrote TextBox 1&2 with 0's, leaving TextBox 3 = 0. Not sure what is up with my Excel. -Minitman On Tue, 14 Dec 2004 17:05:36 -0500, "Ken Macksey" wrote: Hi This example worked fine for me on a userform. TextBox3.Value = Format(Val(TextBox1.Value) + Val(TextBox2.Value), "$###.00") TextBox1.Value = Format(Val(TextBox1.Value), "$###.00") TextBox2.Value = Format(Val(TextBox2.Value), "$###.00") HTH Ken |
#15
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The + operator is also used in .Net for concatenation. You can use the
Convert class to convert a type to another type as Convert.ToInt32(string) with regards, J.V.Ravichandran - http://www.geocities.com/ jvravichandran - http://www.411asp.net/func/search? qry=Ravichandran+J.V.&cob=aspnetpro - http://www.southasianoutlook.com - http://www.MSDNAA.Net - http://www.csharphelp.com - http://www.poetry.com/Publications/ display.asp?ID=P3966388&BN=999&PN=2 - Or, just search on "J.V.Ravichandran" at http://www.Google.com *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Why does this code not work? | Excel Discussion (Misc queries) | |||
Help getting code to work. | Excel Programming | |||
Why my code do not work : - ( | Excel Programming | |||
Why my code do not work : - ( | Excel Programming | |||
Why my code do not work : - ( | Excel Programming |