+ Reply to Thread
Results 1 to 4 of 4

Refresh an already declared variable

  1. #1
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Refresh an already declared variable

    I have a macro in a master file which opens other smaller files and copies their data, it then moves these smaller files into a 'Done' folder with the current date and time appended to the file name.

    Currently I have declared my DateTime variable at the start of the code before my For Each loop gets to work on the files. This means that the the same Time (hhmmss) is appended to each file. Now I assume I could just rearrange the code so the declaration is inside the loop so the Time variable (or at least the seconds) would be updated for each file, but is that the only way to get the different times on each file, or is there some sort of 'refresh' line I can add to the start of the loop that will update the variable value instead?

    And on a related note, what is 'best practice' for declaring variables? Should they ideally all be declared at the start of the code or does it make no difference when or where you declare them (pending their usage obviously)?
    Last edited by EchoPassenger; 09-18-2016 at 04:13 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,205

    Re: Refresh an already declared variable

    Where you declare (Dim) your variable is unimportant as long as it is before the first time it is referred to (used). Just use
    Please Login or Register  to view this content.
    within the loop.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Refresh an already declared variable

    I assume I could just rearrange the code so the declaration is inside the loop
    Simplest.

    Best practice for declaring variables is to declare them at the lowest possible scope. If they are local to a procedure then declare at the beginning of the procedure before they are referenced in code (Declaration, not assigning a value, that can be done anywhere). If they are local to a module, then at the top of the module, and finally, Public variable are declared in standard code modules (only), but before module level variables (Just my preference to keep all 'related' variables together - it doesn't matter where they are declared as long as they are in the General Declarations section at the top of the module).

  4. #4
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Re: Refresh an already declared variable

    Thanks both.

    Quote Originally Posted by EchoPassenger View Post
    I assume I could just rearrange the code so the declaration is inside the loop
    Quote Originally Posted by cytop View Post
    Simplest.
    Are there any issues with repeatedly declaring the same variable in a loop? Is it simply 'replaced' in memory each time?

+ 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] Variable declared as long out as text
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 09:06 AM
  2. [SOLVED] Global variable declared in Userform. Variable value is not sticking
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-25-2012, 08:50 AM
  3. Non-declared variable
    By mqdias in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2007, 10:01 AM
  4. [SOLVED] Refresh Query based on variable
    By GregR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2006, 06:25 PM
  5. [SOLVED] variable not declared error & syntax error
    By G. Beard in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2005, 05:05 PM
  6. Help on combining declared variable and cell formula
    By sylink in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2005, 06:05 PM
  7. [SOLVED] Help on combining declared variable and cell formula
    By sylink in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2005, 03:05 PM

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