+ Reply to Thread
Results 1 to 3 of 3

2 bits of code - different result. why?

  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

    Please Login or Register  to view this content.
    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:

    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    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