Consulting

Results 1 to 5 of 5

Thread: Solved: I'm not understanding why these loops are working in the opposite direction

  1. #1
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location

    Solved: I'm not understanding why these loops are working in the opposite direction

    Edited and swapped the loop result descriptions twice
    I'm confused and need more rest I guess. Or more coffee

    What is different in these two loops?

    this loop reports as I would expect (from Cell A1 to the last row in Col A)
    [vba]For Each aCell In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row - 1)

    MsgBox aCell.Row

    Next[/vba]
    Why is this looping technique reporting the last row number first
    [vba]For X = 0 To Range("A" & Rows.Count).End(xlUp).Row - 1

    MsgBox X

    Next
    [/vba]
    Last edited by frank_m; 03-06-2011 at 07:57 PM.

  2. #2
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Another Correction to my comments: Both loops are starting from the last row and working their way up.
    What am I not realizing ?

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Frank,

    With data down to (inclusive) A28, they both loop from low to high, ending at 27, due to .Row - 1.

    Mark

  4. #4
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Hi Mark,

    Thanks for testing it, rather than just reporting to me that I'm crazy.

    I had declining cell values that I was confusing with row numbers. -- That combined with not not enough sleep

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Trust me, I am most empathetic. I've been on graves for the past year; words for which I could not use here...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •