+ Reply to Thread
Results 1 to 10 of 10

Declare and Initialize on the Same Line

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    10

    Declare and Initialize on the Same Line

    I'm trying to declare and initialize a variable on the same line. Essentially I'm trying to combine these two lines:

    Dim quantity As Integer
    i = 10

    ...into a single line. I know it's possible by saying "Dim i As Integer: i = 6", but that's cheating; it's not really a single statement. Plus, it's not as readable, and it forces me to type "quantity" twice. With long variable names that's a pain.

    I've read on http://msdn.microsoft.com/en-us/library/7ee5a7s1.aspx that you can say:

    Dim quantity As Integer = 10

    ...but that doesn't work for me. There's a compile-time error. The VBA editor highlights the line in red as I type it, and if I try to run the code I get "Compile error: Syntax error." Anybody know what's going on? I assume msdn.microsoft.com should be a reliable source... why isn't their code sample working?

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Declare and Initialize on the Same Line

    You're almost there...

    Try...

    Please Login or Register  to view this content.
    Note: The link you reference is VB, not VBA which could be the difference.
    HTH
    Regards, Jeff

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Declare and Initialize on the Same Line

    aelias,

    Unfortunately that is the incorrect syntax for VBA. That will only work with vb.net which is very similar but not the same.


    e/
    Note: The link you reference is VB, not VBA which could be the difference.
    my bad Jeffrey, didn't see this statement.
    Last edited by Solus Rankin; 08-30-2013 at 05:34 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Registered User
    Join Date
    07-10-2013
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Declare and Initialize on the Same Line

    You're almost there...

    Try...

    [Select Code] copy to clipboard

    Dim quantity As Integer: i = 10

    Note: The link you reference is VB, not VBA which could be the difference.
    I explained why that code is not a solution. I'll look into the VB vs VBA differences.

  5. #5
    Registered User
    Join Date
    07-10-2013
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Declare and Initialize on the Same Line

    I've looked into it a bit more, but I still can't find a VBA Dim statement reference page (similar to the VB Dim statement reference page I've linked to above). Anybody know of one?

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Declare and Initialize on the Same Line

    Why doesn't Jeffrey's solution work? Why is it necessary to do it in one FPC?

  7. #7
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Declare and Initialize on the Same Line

    Quote Originally Posted by aelias3 View Post
    I know it's possible by saying "Dim i As Integer: i = 6", but that's cheating; it's not really a single statement.
    Sorry, missed the part above, but between me and you, I'm a cheater.

    Not sure I know where to point you for any more info on this subject.

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,618

    Re: Declare and Initialize on the Same Line

    The only way I know of is to declare a CONSTANT:

    Const FixedForever as Long = 100
    Ben Van Johnson

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Declare and Initialize on the Same Line

    You simply cannot do that in VBA (or VB6) with a variable.
    Remember what the dormouse said
    Feed your head

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

    Re: Declare and Initialize on the Same Line

    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. [SOLVED] How to declare variables
    By nav505 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2013, 01:25 PM
  2. How do I declare and initialize an array?
    By Corey R. in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2009, 05:04 PM
  3. Why Declare Immediatly?
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-02-2007, 12:50 PM
  4. [SOLVED] Hi All - Declare Decimals
    By Andibevan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2005, 10:05 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