+ Reply to Thread
Results 1 to 3 of 3

The macro works - but why?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2008
    Posts
    3

    The macro works - but why?

    Hi,

    I have some code that I found and modified for my workbook but I can't figure out what exactly is going on. Normally I can put two and two together but here I am stumped, even though it is only a few lines. If anyone could explain to me what the key command function are I would be most appreciative.

    This macro groups like entries as defined in Column A by inserting blank rows between the data, for example:

    Before:

    ITEM 1
    ITEM 1
    ITEM 1
    ITEM 2
    ITEM 2
    ITEM 3

    After:

    ITEM 1
    ITEM 1
    ITEM 1

    ITEM 2
    ITEM 2

    ITEM 3

    And here is the relevant code:

     Dim i As Long
        For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Cells(i, "A") <> Cells(i - 1, "A") _
                And Cells(i, "A") <> "" _
                And Cells(i - 1, "A") <> "" Then
                Rows(i).Insert
            End If
        Next
    Could someone please interpret what each line is doing?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Starting with the last used cell in column A,

    If the cell above is not the same as the current cell, and neither cell is blank, insert a row above the current cell.

    Move up one row and repeat until you get to row 2. (If you went all the way to row 1, there would be nothing to compare the current cell with.)

  3. #3
    Registered User
    Join Date
    02-26-2008
    Posts
    3
    Thanks shg.

    I kept thinking this was going top down which is why it didn't make any sense to me. It's all very clear now!

+ 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