+ Reply to Thread
Results 1 to 7 of 7

Reset variables for each pass through a For loop

  1. #1
    Registered User
    Join Date
    05-02-2014
    Location
    Birmingham, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    15

    Reset variables for each pass through a For loop

    I am having trouble defining a variable in a For loop. When I run the loop the first time everything seems to work fine, but when the loop goes back through the second time it carries its previous value with it. Here in lies the problem if the variable is supposed to be empty (not assigned a value). So, for example: 'Variable' in the code below may or may not be given a value depending on the conditions in the For i loop. If it is given a value then 'Variable' will still have that value on the next pass through the For j loop. And if none of the conditions are met for the If statement in the For i loop the second time through the For j loop, it should be an empty variable, but instead the code is reading it as having a value (from the previous pass through the For j loop).

    Please Login or Register  to view this content.
    I have tried using
    Please Login or Register  to view this content.
    after the For j line, and instead of using
    Please Login or Register  to view this content.
    I tried using
    Please Login or Register  to view this content.
    and I've tried
    Please Login or Register  to view this content.
    .
    I get an Object error with either line I use. I just want to be able to reset the variables each time through the For j loop so in the case of an empty variable, it doesn't try to calculate based on the previous value.

    Any help on the issue will be greatly appreciated.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Reset variables for each pass through a For loop

    Can you post the actual code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-02-2014
    Location
    Birmingham, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Reset variables for each pass through a For loop

    Thank you for the quick reply. Here is some actual code. There are several For i loop sections, but I just included one. Hopefully this will be of help.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Reset variables for each pass through a For loop

    Which variable(s) do you want to 'reset'?

    Are you sure you actually need to do that for any variable?

    By the way, this won't work as RelThrReactTime is not an object.
    Please Login or Register  to view this content.
    If you want to check if RelThrReactTime is empty/null/zero you'll need to use something else, what that something else is will depend on how you've declared RelThrReactTime and what you expect it's value to be.

    Same thing here.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-02-2014
    Location
    Birmingham, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Reset variables for each pass through a For loop

    You are correct in assuming variables such as RelThrReactTime are the ones I was thinking about clearing for each pass through the For j loop. I wouldn't necessarily think I should need to clear the variables, but because on one pass through the For j loop a variable may be given a value, and on the next pass say it shouldn't be given a value (based on the logical statement in the For i loop), but the code still reads it as having a value because of the previous pass through the For j loop. So on any pass through the For j loop it is possible a variable will or will not be assigned a value. And based on the variable having or not having a value in the current pass, I print certain values in a given cell. I have not declared the variables previously, so I will do so. If I declare them as long, how do you recommend I proceed?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Reset variables for each pass through a For loop

    If you've declared a variable as long to 'reset' it set it to 0.
    Please Login or Register  to view this content.
    Then you can check if it's 0.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-02-2014
    Location
    Birmingham, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Reset variables for each pass through a For loop

    Of course. I don't why I didn't think of such a simple solution. Thank you for all your help.

+ 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. Excel 2007 : Cant pass variables properly
    By grizzlypete44 in forum Excel General
    Replies: 4
    Last Post: 12-01-2011, 09:51 AM
  2. cant pass variables between controls
    By grizzlypete44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2011, 03:14 PM
  3. Public Variables still being reset
    By Mervil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-26-2010, 11:25 AM
  4. [SOLVED] Pass environmental variables?
    By Gunkie in forum Excel General
    Replies: 2
    Last Post: 03-07-2005, 07:06 PM
  5. [SOLVED] Pass variables value to Shell
    By wpw3 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-07-2005, 12:06 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