Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Beginner Variable Problem

First Day Learning VBA for Excel. I've written the following su
routine,

Option Explicit

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = Input1 + Input2
MsgBox "The total amount is " & Total
End Sub

Why does the Total Amount of 2+2 display as 22? If I change th
operators to * , / , or - , I get the correct arithmetic answer. Wit
the + I get concatenation instead of addition. Curious.
Cheers
Dodu

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Beginner Variable Problem

Interesting that I can't spell my own name!
Dou

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Beginner Variable Problem

Doug,

It's treating the input as strings and the + operator is concatenating the
strings, not adding the value of those strings. You could try

Total = CDbl(Input1) + CDbl(Input2)

or declare the variables as double

Dim Input1 As Double
Dim Input2 As Double

Either way you should add some error handling to ensure you don't get bad
input.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"doc_in_bc " wrote in message
...
First Day Learning VBA for Excel. I've written the following sub
routine,

Option Explicit

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = Input1 + Input2
MsgBox "The total amount is " & Total
End Sub

Why does the Total Amount of 2+2 display as 22? If I change the
operators to * , / , or - , I get the correct arithmetic answer. With
the + I get concatenation instead of addition. Curious.
Cheers
Dodug


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Beginner Variable Problem

Because you have not specified the type of input variable, input box
defaults to a string so Input1 + Input2 is concatentating "2" + "2"
Change the Dim lines to

Dim Input1 as Double
Dim Input2 as Double

This tells VBA to expect a numerical value. Caution do not rely on this
method, if the user enters anything other than a numerical value you will
get an error.

Cheers
Nigel

"doc_in_bc " wrote in message
...
First Day Learning VBA for Excel. I've written the following sub
routine,

Option Explicit

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = Input1 + Input2
MsgBox "The total amount is " & Total
End Sub

Why does the Total Amount of 2+2 display as 22? If I change the
operators to * , / , or - , I get the correct arithmetic answer. With
the + I get concatenation instead of addition. Curious.
Cheers
Dodug


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Beginner Variable Problem

Doug

InputBox returns a string, and when using "+" VBA apparently
uses concatenation on strings instead of summing as the default
operation. Since "-", "*" and "/" can't be used for string manipulation,
Excel try to convert the strings before carrying out the calculation
in these instances.

Two ways to overcome it:

Dimensioning the variables explicitly.
Dim Input1, dimensions Input1 as the variable type "Variant",
which will hold, what's put into it :-)

Sub SunnyDay()
Dim Input1 As Double 'Or any other numeric type
Dim Input2 As Double
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = Input1 + Input2
MsgBox "The total amount is " & Total
End Sub



Converting the string to a numeric type:

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = CDbl(Input1) + CDbl(Input2)
MsgBox "The total amount is " & Total
End Sub


Actually it's only necessary to convert one of the
values. Excel will get the drift :-)

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = CDbl(Input1) + Input2
MsgBox "The total amount is " & Total
End Sub




--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"doc_in_bc " skrev i en
meddelelse ...
First Day Learning VBA for Excel. I've written the following sub
routine,

Option Explicit

Sub SunnyDay()
Dim Input1
Dim Input2
Dim Total
Input1 = InputBox("Enter the first amount!")
Input2 = InputBox("Enter the second amount!")
Total = Input1 + Input2
MsgBox "The total amount is " & Total
End Sub

Why does the Total Amount of 2+2 display as 22? If I change the
operators to * , / , or - , I get the correct arithmetic answer. With
the + I get concatenation instead of addition. Curious.
Cheers
Dodug


---
Message posted from http://www.ExcelForum.com/






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Beginner Variable Problem

Thanx bunches ... makes sense!
Cheers
Dou

--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Beginner Variable Problem

You're welcome and thanks for the feedback :-)

LeoH


"doc_in_bc " skrev i en
meddelelse ...
Thanx bunches ... makes sense!
Cheers
Doug



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with a defined variable ric Excel Programming 2 April 26th 04 08:42 PM
Object variable problem ExcelMonkey[_13_] Excel Programming 4 January 23rd 04 08:39 PM
vba variable problem joao Excel Programming 3 December 15th 03 04:31 PM
Mixed variable problem Tom Excel Programming 1 November 7th 03 09:22 PM
Problem trying to us a range variable as an array variable TBA[_2_] Excel Programming 4 September 27th 03 02:56 PM


All times are GMT +1. The time now is 03:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"