+ Reply to Thread
Results 1 to 3 of 3

2 bits of code - different result. why?

Hybrid View

tigertim71 2 bits of code - different... 07-29-2008, 03:25 PM
Richard Buttrey Hi, The default value for... 07-29-2008, 03:36 PM
tigertim71 Thanks 07-29-2008, 03:44 PM
  1. #1
    Registered User
    Join Date
    05-04-2008
    Posts
    43

    2 bits of code - different result. why?

    Sub t334()
    Dim x, y, z As Integer
    x = 3
    y = 3
    z = x + y
    MsgBox z
    End Sub
    The answer to this code is 6

    Sub test6()
    Dim int1, int2, answer As Integer
    int1 = InputBox("please type a whole number")
    int2 = InputBox("please type another whole number")
    answer = int1 + int2
    MsgBox answer
    End Sub
    If I enter 3 in int1, and 3 in int2 I get 33. Does anyone know why? I would like it to add like in the first example and get 6...

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    The default value for an Input Box is a string. You're adding two strings together. If you change the line of code to:

    answer = CInt(int1) + CInt(int2)
    You'll get 6.

    Incidentally did you realise that although you can declare several variables on one Dim line as you have done, you still need to define the type. i.e.

    Dim int1 as Integer, int2 As Integer, answer As Integer
    If you don't all but the last one are Variant variables

    HTH
    Last edited by Richard Buttrey; 07-29-2008 at 04:08 PM.

  3. #3
    Registered User
    Join Date
    05-04-2008
    Posts
    43

    Thanks

    OK- Many Thanks. I've resolved this now and understand what I did wrong.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1