+ Reply to Thread
Results 1 to 11 of 11

For Next Statement with If Statement Reducing Upper Variable

  1. #1
    Registered User
    Join Date
    10-27-2012
    Location
    London
    MS-Off Ver
    Excel 2007-2010
    Posts
    59

    For Next Statement with If Statement Reducing Upper Variable

    Hi,

    Hope you can help, below I am trying to run through each sheet in an activeworkbook and perform code. Some of the code will delete an activesheet if it meets the requirements. I have assigned the upper variable as SheetsCount, this can be reduced by a sheet being deleted in the code, however my code below keeps running passed the redefined upper limit variable SheetsCount. So after a sheet is deleted and SheetsCount is redefined as -1 its orginal value the code keeps running passed the new value of SheetsCount.

    I'm obviously missing something basic, and Ideas?

    Please Login or Register  to view this content.
    Sorry I might not have explained this very well, but have given a basic example of my error in the code above.

    Any input / ideas welcomed!

  2. #2
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: For Next Statement with If Statement Reducing Upper Variable

    Hi,

    What is this for n = n - 1.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  3. #3
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: For Next Statement with If Statement Reducing Upper Variable

    This will do what you want.
    Please Login or Register  to view this content.

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

    Re: For Next Statement with If Statement Reducing Upper Variable

    loop backwards
    Please Login or Register  to view this content.
    Josie

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

  5. #5
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: For Next Statement with If Statement Reducing Upper Variable

    Here you go.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-27-2012
    Location
    London
    MS-Off Ver
    Excel 2007-2010
    Posts
    59

    Re: For Next Statement with If Statement Reducing Upper Variable

    When the sheet is deleted, excel would place us on the next sheet which would actually be n + 1 in this situation, so I reset it at this stage. I'll have a look if this logic flows a bit more!

  7. #7
    Registered User
    Join Date
    10-27-2012
    Location
    London
    MS-Off Ver
    Excel 2007-2010
    Posts
    59

    Re: For Next Statement with If Statement Reducing Upper Variable

    Thanks will give it a go. All very helpful, thanks.

    I think I will need to brush up on my knowledge of Step statements. Thanks.
    Last edited by Break_Point; 02-27-2013 at 06:34 PM.

  8. #8
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: For Next Statement with If Statement Reducing Upper Variable

    Just looking at the code (it may change the sheet that is active) but your code decrements what you are counting to (correct) but then it decrements the counting variable n (which is wrong). The program changes active sheet but does not automatically update your n to n+1.

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

    Re: For Next Statement with If Statement Reducing Upper Variable

    it is standard practice when deleting objects to loop backwards through the collection to obviate any need to alter index variables as you go

  10. #10
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: For Next Statement with If Statement Reducing Upper Variable

    Hi,

    the biggest problem here is the way For Loop was meant to run.

    Please Login or Register  to view this content.
    It runs 10 times, even though in the for loop we have changed the value of ctr to 1.

    The value of ctr remains static. It simply retains the value it was assigned at the start of the FOR LOOP.

    So not a good idea to use a For Loop in such scenarios.

    Hope this helps.

  11. #11
    Registered User
    Join Date
    10-27-2012
    Location
    London
    MS-Off Ver
    Excel 2007-2010
    Posts
    59

    Re: For Next Statement with If Statement Reducing Upper Variable

    Thanks for all the info and help.

    I followed your input and looped backwards and this has resolved my issue.

    Great advice and info from all, thanks.

+ 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