+ Reply to Thread
Results 1 to 2 of 2

Integer, Long and As Variant - when would you use them and examples?

  1. #1
    Forum Contributor
    Join Date
    09-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2010
    Posts
    132

    Integer, Long and As Variant - when would you use them and examples?

    Hi,

    I'm slowly getting the hang of VBA, however when asking for help for the excel experts on here they're giving me integer, long and as variant codes and i'm not exactly sure what they mean. I have a brief idea of integer and long, however not sure what as variant is.

    Any help/brief descriptions?

    Cheers

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Integer, Long and As Variant - when would you use them and examples?

    Search Help for "data type summary"

    You will also find the explanation for Variant

    Variant Data Type

    The Variant data type is the data type for all variables that are not explicitly declared as some other type (using statements such as Dim, Private, Public, or Static). The Variant data type has no type-declaration character.

    A Variant is a special data type that can contain any kind of data except fixed-length String data. (Variant types now support user-defined types.) A Variant can also contain the special values Empty, Error, Nothing, and Null. You can determine how the data in a Variant is treated using the VarType function or TypeName function.

    Numeric data can be any integer or real number value ranging from -1.797693134862315E308 to -4.94066E-324 for negative values and from 4.94066E-324 to 1.797693134862315E308 for positive values. Generally, numeric Variant data is maintained in its original data type within the Variant. For example, if you assign an Integer to a Variant, subsequent operations treat the Variant as an Integer. However, if an arithmetic operation is performed on a Variant containing a Byte, an Integer, a Long, or a Single, and the result exceeds the normal range for the original data type, the result is promoted within the Variant to the next larger data type. A Byte is promoted to an Integer, an Integer is promoted to a Long, and a Long and a Single are promoted to a Double. An error occurs when Variant variables containing Currency, Decimal, and Double values exceed their respective ranges.

    You can use the Variant data type in place of any data type to work with data in a more flexible way. If the contents of a Variant variable are digits, they may be either the string representation of the digits or their actual value, depending on the context. For example:

    Dim MyVar As Variant
    MyVar = 98052


    In the preceding example,

    MyVar

    contains a numeric representation — the actual value
    98052

    . Arithmetic operators work as expected on Variant variables that contain numeric values or string data that can be interpreted as numbers. If you use the + operator to add
    MyVar

    to another Variant containing a number or to a variable of a numeric type, the result is an arithmetic sum.

    The value Empty denotes a Variant variable that hasn't been initialized (assigned an initial value). A Variant containing Empty is 0 if it is used in a numeric context and a zero-length string ("") if it is used in a string context.

    Don't confuse Empty with Null.Null indicates that the Variant variable intentionally contains no valid data.

    In a Variant, Error is a special value used to indicate that an error condition has occurred in a procedure. However, unlike for other kinds of errors, normal application-level error handling does not occur. This allows you, or the application itself, to take some alternative action based on the error value. Error values are created by converting real numbers to error values using the CVErr function.

    © 2010 Microsoft Corporation. All rights reserved.
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Integer defined array vs variant defined array ..error making assignment to integer
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2011, 01:48 PM
  2. Converting variant return type from evaluate to integer
    By PJN4 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-07-2009, 12:16 PM
  3. assigning a Variant variable to an integer
    By jartzh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-02-2009, 05:06 PM
  4. variant to integer format?
    By James Cornthwaite in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2006, 04:40 PM
  5. Variant to integer
    By Zurn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2005, 06:08 AM

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