VBA Data Types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    VBA Data Types

    Code:
    [B][U]Type[/U]        [U]MemSize[/U] [U]RetVal of VarType()[/U] [U]Declaration Char[/U]   [U]Conversion[/U][/B]
    Boolean     2b      vbBoolean(11)                          CBool()
    Byte        1b      vbByte(17)                             CByte()
    Currency    8b      vbCurrency(6)       @                  CCur()
    Date        8b      vbDate(7)                              CDate()
    Decimal     14b     vbDecimal(14)                          CDec()
    Integer     2b      vbInteger(2)        %                  CInt()
    Long        4b      vbLong(3)           &                  CLng()
    LongLong    8b      vbLongLong(20)      ^                  CLngLng()
    Single      4b      vbSingle(4)         !                  CSng()
    Double      8b      vbDouble(5)         #                  CDbl()
    String      10b+    vbString(8)         $                  CStr()
    Object      4b      vbObject(9)
    Variant     16b     vbVariant(12)                          CVar()

    The Boolean Data Type (True or False)
    Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and false, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values –1 and 0, respectively.

    The Byte Data Type (0 to 255)
    Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't support negative values. If you attempt to assign one, VBA returns an error.

    The Currency Data Type
    Use the Currency numeric data type to store monetary values from –922,337,203,47 7.5808 to 922,337,203,685 ,477.5807.

    The Date Data Type
    The Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

    The Decimal Data Type
    The Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from –79,228,162,514 ,264,337,593,54 3,950,335 to 79,228,162,514, 264,337,593,543 ,950,335 if the value contains no decimal places. The data type maintains precision up to 28 decimal places with values from –7.922816251426 433759354395033 5 to 7.9228162514264 337593543950335 .

    The Integer Data Type
    This is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from –32,768 to 32,767.

    The Long Data Type
    The Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648 to 2,147,483,647.

    The LongLong Data Type (64-bit only)
    The LongLong data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer & Long data types. Use the LongLong data type to store values from -9,223,372,036,8 54,775,808 to 9,223,372,036,8 54,775,807.

    The Single Data Type
    The Single data type stores precision numbers—numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

    The Double Data Type
    Use the Double data type to store precision floating point numbers from –1.797693134862 32E308 to –4.940656458412 47E-324 or 1.7976931348623 2E308 to 4.9406564584124 7E-324.

    The String Data Types (Fixed & Variable lengths)
    String is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. The Dim for a Fixed String is of the form:
    Dim variablename As String * stringlength

    In contrast, the Variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form
    Dim variablename As String

    The Object Data Type
    An Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module.

    The Variant Data Type
    The Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications.

    The Variant data type is VBA's default, so the following code interprets varValue as a Variant:
    Dim varValue

    Although the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit.

    The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't easily determine the appropriate data type by viewing the code, and that can be a problem.

    Note:
    Please see Matthew's post for information on updates in VBA7 for 64-bit arithmetic and pointer compatibility.

    NB:
    The LongLong data type has now been added in as well as a table showing some further helpful information.
    Last edited by NeoPa; Oct 23 '21, 04:42 PM. Reason: Extra information including for 64 bit.
  • matthewslyman
    New Member
    • Nov 2006
    • 20

    #2
    Limits wrong...

    The Long Data Type
    2,147,486,647 should read 2,147,483,647
    (Open Windows Calculator, select scientific view, and try 2^31 - 1, to get this result. It's a 32-bit signed integer.)

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Thanks, I'll edit this in the original :) Probably just a type originally.

      Mary

      Comment

      • Appu George

        #4
        Good notes, thanks

        Comment

        • Matthew Slyman

          #5
          VBA7 : New data types

          Updates in VBA7 for 64-bit arithmetic and pointer compatibility:





          Note, for example, the new 64-bit LongLong type and various Ptr types.

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Thank you for that Matthew. I've edited the original article to add a link to your post.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              I've just taken the liberty (Sorry Mary) of updating the OP of this, still very popular, thread to bring it up-to-date with the main new 64 bit Long Long data type mentioned previously in the discussion. I also took the opportunity, while I was about it, to add in a table at the top showing some often useful information about the data types included here.

              Comment

              • svtsolutionllc
                New Member
                • Nov 2021
                • 2

                #8
                Good one
                Noted!
                Last edited by NeoPa; Nov 4 '21, 04:04 AM. Reason: Not deleted but seen as suspicious activity. Member added to watch list.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32662

                  #9
                  I wanted to add a link here (Type Characters) to an article on this subject from Microsoft for further reading if required.

                  Comment

                  Working...