+ Reply to Thread
Results 1 to 4 of 4

Assigning a string to an integer variable...why it "works" sometimes?

  1. #1
    Registered User
    Join Date
    08-08-2012
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    15

    Assigning a string to an integer variable...why it "works" sometimes?

    I have been using what's in Code Sample 2 (to be unveiled after Code Sample 1). Then while trying to figure out how stuff works, I wrote a tester Sub:

    Code Sample 1:
    Please Login or Register  to view this content.
    The above code gives me an error if I have a non numeric in A1. This is what I expect. Then why does the below code (which I wrote when i was naive...a few months ago) NOT give me the same error? You can see I even added a little MsgBox to talk to me AFTER it processes a non numeric. I have thrown random strings (asdfasd) at the following code without messing it up.

    In Sample 1, I can change it to Variant, for example, and it works fine. But why does Sample 2 seem to be treating it as Variant? What's VBA doing that I'm not seeing?


    Code Sample 2:
    Please Login or Register  to view this content.
    Thanks

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Assigning a string to an integer variable...why it "works" sometimes?

    current is a Variant. If you want it to be an integer, you need to explicitly state so in the Dim line:
    Please Login or Register  to view this content.
    Because you left out the type declaration, it defaulted to be a variant.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Assigning a string to an integer variable...why it "works" sometimes?

    To expand, here's a good explanation taken from here:
    http://www.cpearson.com/excel/DeclaringVariables.aspx

    Pay Attention To Variables Declared With One Dim Statement

    VBA allows declaring more than one variable with a single Dim statement. I don't like this for stylistic reasons, but others do prefer it. However, it is important to remember how variables will be typed. Consider the following code:

    Dim J, K, L As Long

    You may think that all three variables are declared as Long types. This is not the case. Only L is typed as a Long. The variables J and K are typed as Variant. This declaration is functionally equivalent to the following:

    Dim J As Variant, K As Variant, L As Long

    You should use the As Type modifier for each variable declared with the Dim statement:

    Dim J As Long, K As Long, L As Long

  4. #4
    Registered User
    Join Date
    08-08-2012
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Assigning a string to an integer variable...why it "works" sometimes?

    Thank you both. Makes sense now. I didn't even think of that/know to think of it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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