+ Reply to Thread
Results 1 to 7 of 7

Declaring and naming a variable

  1. #1
    Registered User
    Join Date
    10-19-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    59

    Declaring and naming a variable

    I read that you can declare and name your variable in the same line. However I keep getting a "Compile Error: Expected: End of Statement" error when I hit enter.

    Please Login or Register  to view this content.
    The Compile error highlights the "=" sign.

    Why is that?

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Declaring and naming a variable

    Perhaps your book is saying this?
    Please Login or Register  to view this content.
    Last edited by MarvinP; 07-30-2012 at 03:04 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

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

    Re: Declaring and naming a variable

    If you know that anInteger will never change then:
    Const AnInteger=42
    However if AnInteger is a variable which may assume different values during the program then:
    Dim AnInteger as Integer


    then load it with a starting value:
    AnInteger=42
    Last edited by protonLeah; 07-30-2012 at 03:05 PM.
    Ben Van Johnson

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

    Re: Declaring and naming a variable

    ExcelAteMyHomework,

    In Visual Basic.NET you can declare a variable and assign it a value on a single line as you've shown. In Visual Basic for Applications (VBA) that is not the case (unless it is a constant declared with Const as portonLeah pointed out). There is a workaround that MarvinP has shown though. The colon ( : ) symbol is being used to tell the compiler that the Dim line is complete, and that any remaining text should be treated as a new line.
    Last edited by tigeravatar; 07-30-2012 at 03:05 PM.
    Hope that helps,
    ~tigeravatar

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

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Declaring and naming a variable

    Hi,

    True to a point.

    Dim anInteger As Integer : anInteger = 42

    Although I personally don't consider that as 'the same line'
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  6. #6
    Registered User
    Join Date
    10-19-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Declaring and naming a variable

    Quote Originally Posted by tigeravatar View Post
    ExcelAteMyHomework,

    In Visual Basic.NET you can declare a variable and assign it a value on a single line as you've shown. In Visual Basic for Applications (VBA) that is not the case (unless it is a constast declared with Const as portonLeah pointed out). There is a workaround that MarvinP has shown though. The colon ( symbol is being used to tell the compiler that the Dim line is complete, and that any remaining text should be treated as a new line.
    Thank you so much. I'm really trying to learn this thing and I wont give up! I guess it would help if I learned what I was working in first!

    Thanks to MarvinP and Proton for responding as well.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Declaring and naming a variable

    I agree with Richard and prefer not to use the colon separate. BUT it did get it onto the same line as suggested.

    Read a little of http://www.cpearson.com/excel/declaringvariables.aspx to see how using commas in a DIM line is confusing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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