Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Problem with a very simple code in Visual basic

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Problem with a very simple code in Visual basic

The problem is that A and B as string (text) variables, and the
"+" sign is the old way of concatenating (combining) strings.
Instead, try,

Dim A As Double
Dim B As Double
Dim C As Double
A = Application.InputBox(prompt:="Enter A:", Type:=1)
B = Application.InputBox(prompt:="Enter B:", Type:=1)
C = A + B

The Type:=1 argument restricts users to entering numbers. It
prevents non-numeric entry.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Vepa" wrote in message
...
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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Problem with a very simple code in Visual basic

Because input box returns string, not number, so conatenation of 3 and 4
gives 34.
Declare variables

dim a as double, b as double

or dim a as integer, b as integer.

It will convert it to numbers, however it will crash when user enters
not numbers.

So you can:

dim A as string, b as string

dim iA as integer, iB as integer

and using isNumber you can check if data enterd by user is a number and
after that convert it to integer

iA = cInt (A)







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


  #4   Report Post  
Posted to microsoft.public.excel.programming
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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Problem with a very simple code in Visual basic

Thank you for your advices. They helped me a lot. Now back to learn more..

"brucks" kirjoitti:

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


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
A simple visual basic program wilchong via OfficeKB.com New Users to Excel 2 October 22nd 08 09:16 AM
Visual Basic code Pickle Excel Discussion (Misc queries) 1 September 4th 08 03:35 PM
I need a visual basic code....please Rhonda Excel Discussion (Misc queries) 1 March 5th 07 01:18 PM
Need code for Excel Simple Visual Basic Macro to select next avai. Marco Margaritelli[_3_] Excel Programming 6 November 2nd 04 10:26 PM
Visual Basic Code Robert Couchman Excel Programming 2 February 2nd 04 03:10 PM


All times are GMT +1. The time now is 06:31 AM.

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"