0

Im Totally new to VBA.. Im trying to Loop through a column's rows from last filled row. I have googled about this but I could find code that iterates only from the top.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
CodeKhaleesi
  • 79
  • 1
  • 1
  • 8
  • 4
    [Here](https://stackoverflow.com/questions/38314573/running-a-loop-from-bottom-to-top), [here](https://stackoverflow.com/questions/38311477/bottom-to-top-loop-deletes-every-row-in-excel-regardless-if-the-if-requirements), [here too](https://stackoverflow.com/questions/24358183/reverse-order-of-for-each-loop).... and so on. – Damian May 06 '19 at 10:30

1 Answers1

0

You need to populate a variable with the last row and use Step -1, which indicates the direction

Sub loopbackwards()
Dim i As Long, LRow As Long

With Workbooks(REF).Sheets(REF)

    LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LRow to 2 Step -1 'i represents the row nr.
        Debug.Print i
        'or do stuff
    Next i

End With

End Sub
Tim Stack
  • 3,209
  • 3
  • 18
  • 39
  • Tim you forgot to declare **i**. To avoid this use **Option Explicit** – Error 1004 May 06 '19 at 11:08
  • I "forgot" to declare LRow too didn't I? – Tim Stack May 06 '19 at 11:12
  • yes. i forgot to mention that. Also have in mind that both **REFs** in your code should surrounded by double quotes – Error 1004 May 06 '19 at 11:13
  • I'm absolutely not going to double qoute REF. Users need to reference to their own wb/ws. Inexperienced users are less likely to think double quoted REFs need editing. – Tim Stack May 06 '19 at 11:31
  • I agree with the `Users need to reference to their own wb/ws` but what you wrote has no sense. If he is going to reference, he will use `With ws`, who would reference a sheet name if it doesn't come from another procedure? Even then, you would set the worksheet object, not a string. – Damian May 06 '19 at 11:34
  • I've used REF in my answers for ages, never has anyone had issues with it. I understand the both of you, but bit of a useless discussion this :) – Tim Stack May 06 '19 at 11:35
  • Every question is different & unique. the OP starts with *Im Totally new to VBA.* – Error 1004 May 06 '19 at 11:38