+ Reply to Thread
Results 1 to 7 of 7

VBA Define Variable

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    VBA Define Variable

    Does anyone know why this line of code won't work? I'm trying to define Date as the contents of cell "A2".


    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA Define Variable

    Date is a reserved word in VBA, so it shouldn't be used as a variable name.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Define Variable

    On the right of the assignment operator, Date is a native VBA function that returns the date. On the left, it is a statement that sets the date. Choose a different variable name.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA Define Variable

    I personally find putting 'My' before a variable name helps to ease these problems.

    For example: myDate, myRange, myObject

    Hope this helps.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA Define Variable

    I like the tip from PED - give each variable a prefix letter according to the variable type, e.g.

    lMyVar is a long, iMyVar is an integer, sMyVar is a string ... and so on. Simple to do, but it does make re-reading old code much easier.

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA Define Variable

    Completely agree with Andrew.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Define Variable

    variations of the Hungarian naming conventions are popular http://en.wikipedia.org/wiki/Leszyns...ing_convention
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ 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