View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
brucks brucks is offline
external usenet poster
 
Posts: 1
Default Problem with a very simple code in Visual basic

Try this:

A = Val(InputBox("Enter domestic investment amount"))
B = Val(InputBox("Enter foreign investment amount"))
TotalInvestment = A + B

OR
TotalInvestment = Val(A) + Val(B)

OR
A = InputBox("Enter domestic investment amount",,,,,,1)
B = InputBox("Enter foreign investment amount",,,,,,1)

Either one of these should work. The issue is that the InputBox returns
text values, not numeric unless you specify using the last arguement of "1"
which tells VBA the data being returned is numeric. Also, in VBA, + and &
are concatenation functions for text, which is why you were getting 34
returned.

If you use either of the first two possible solutions, you will need to
check each response to see if they are numeric or not (you could use the
IsNumeric function to do so) before using the Val() function to ensure that
the inputs are indeed numeric in nature.

Hope this helps.

"Vepa" wrote:

Hi,

I'm beginning my studies with Visual Basic on my Excel 2003 and I have a
problem with calculations concerning adding. I will write the source and I
hope somebody could help me. The problem is that the calculation below gives
me a result 34 if A=3 and B=4. However if I try to multiply these numbers the
program gives me the right answer..

Sub Investments()
A = InputBox("Enter domestic investment amount")
B = InputBox("Enter foreign investment amount")
TotalInvestment = A + B
MsgBox "The total investment= " & TotalInvestment
End Sub

Regards
Vepa