none
Beginner's question about data types RRS feed

  • Question

  • Hi!

    I'm a complete VBA noob but my work in excel has motivated me to dive into this stuff.

    So I'm faithfully working my way through these tutorials.

    I have a question about this secion:

    Dim anInteger As Integer = 54
    MsgBox(CStr(anInteger))

    I was trying out some stuff to see if I understood everything. I expected an error when I assign a value that doesn't correspond with a data type to a data type. Something like this.

            Dim anInteger As Integer = False
            MsgBox(CStr(anInteger))

    or

            Dim whatever As Boolean = 6
            MsgBox(CStr(whatever))

    But instead of an error I get a 0 or "True" as response. Why is this?


    Friday, May 11, 2012 12:05 PM

Answers

  • Strictly speaking, this forum is about VBA, the Visual Basic version found in VB6 and in Microsoft Office.

    The code that you posted is for a newer version of Visual Basic (VB.Net / Visual Basic Express).

    But Visual Basic is quite forgiving, it will attempt to convert a value to the required data type if possible.

    When converting a Boolean value to a number, False becomes 0 and True becomes -1.

    When converting a number value to Boolean, 0 becomes False and any non-zero value becomes True.

    You can't mix all data types, though. The following line will cause an error:

    Dim anInteger As Integer = "boerminator"

    The string cannot be converted to a number in a meaningful way.

    Regards, Hans Vogelaar

    • Marked as answer by boerminator Friday, May 11, 2012 12:58 PM
    Friday, May 11, 2012 12:17 PM

All replies

  • Strictly speaking, this forum is about VBA, the Visual Basic version found in VB6 and in Microsoft Office.

    The code that you posted is for a newer version of Visual Basic (VB.Net / Visual Basic Express).

    But Visual Basic is quite forgiving, it will attempt to convert a value to the required data type if possible.

    When converting a Boolean value to a number, False becomes 0 and True becomes -1.

    When converting a number value to Boolean, 0 becomes False and any non-zero value becomes True.

    You can't mix all data types, though. The following line will cause an error:

    Dim anInteger As Integer = "boerminator"

    The string cannot be converted to a number in a meaningful way.

    Regards, Hans Vogelaar

    • Marked as answer by boerminator Friday, May 11, 2012 12:58 PM
    Friday, May 11, 2012 12:17 PM
  • Hello Hans,

    Thanks for your explanation. Sorry for posting this in the wrong place then. I'm diving into VBA but found the tutorials I linked to in my first post very helpful. So I downloaded Visual Basic so I could follow the lessons. The code looked so similar to me that I assumed I could use the knowledge gained in VBA in Excel as well. Is that not the case?

    Your explanation is clear. On the one hand I find this mechanism useful. On the other hand I'd rather be informed when I have linked the 'wrong' (even though it may still work) data type to my data. Oh well, something I'll pay attention to in the future.

    Thanks again,

    Maarten

    Friday, May 11, 2012 12:58 PM
  • VB Express and VBA are similar, but there are significant differences. A line such as

    Dim anInteger As Integer = 37

    is not valid in VBA. You have to use

    Dim anInteger As Integer
    anInteger = 37

    When declaring a constant instead of a variable, you do assign the value:

    Const MyName As String = "Hans"

    Another difference is that the Integer data type in VBA is a 16-bit number ranging from -32,768 to +32,767, while Integer in VB Express is a 32-bit integer ranging from -2,147,483,648 to +2,147,483,647. VBA uses Long Integer to specify a 32-bit number, and VB Express uses Short to specify a 16-bit number.

    Confusing, isn't it?


    Regards, Hans Vogelaar


    Friday, May 11, 2012 1:10 PM
  • Another difference is that the Integer data type in VBA is a 16-bit number ranging from -65,536 to +65,535,

    Even less, the VBA Integer type ranges between only +/-32k. The type dates back to when both Windows OS and Office were 16bit. Since 32bit Office there's no useful reason ever to use the Integer type in preference to a Long (except with pre-built event stubs which should be left unchanged).

    Peter Thornton

    Friday, May 11, 2012 3:10 PM
    Moderator
  • Oops, sorry. You're right, of course! I'll correct my previous reply.

    Regards, Hans Vogelaar

    Friday, May 11, 2012 3:17 PM