
Originally Posted by
Zoediak
... could you further explain or breakdown the code so I can understand how it is working?
here's an annotated version of that code
Sub xxx_annotated()
'Purpose is to delete adjoining rows with duplicate values in ColC,
'retaining only one such row, and
'to copy the value in ColumnI of remaining duplicate row to Col G and then delete Col I value
'Declare the variables to be used
Dim r As Long, c As Long, i As Long
Dim a, s, u()
'Obtain the last row and last column of the used region of the worksheet
r = Cells.Find("*", , , , xlByRows, xlPrevious).Row
c = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
'define the size of the (memory) array later used
'to to mark the rows with duplicates in ColC
ReDim u(1 To r, 1 To 1)
'Put the values of the worksheet data array into
'a memory array for (usually) faster manipulations
a = Cells(1).Resize(r + 1, c)
'the above line can be optionally written a = Cells(1).Resize(r + 1, c).Value
'to maybe remind the user that it refers only to values and not to formats etc.
'the .Value can however be omitted by default
'loop though the memory array to find the number of duplicates in ColC, i.e. s
'also to mark in the u() array those rows in which duplicates occur in ColC
'also does the requested swap (in memory array) of relevant values in cols G and I
For i = 2 To r
If a(i, 3) = a(i - 1, 3) Then
s = s + 1
u(i, 1) = 1
a(i - 1, 7) = a(i - 1, 9): a(i - 1, 9) = ""
End If
Next i
'If number of duplicates in ColC is > 0 then
'list the data array with swapped relevant values of Cols G and I back onto the worksheet
'list the array u() (with duplicate rows marked with 1) into an additional used column
'sort all used rows in the worksheet by the column where duplicates marked
'delete all rows where duplicates occurred as one block
'this is much faster than the row-by-row deletion that is so commonly done, and
'was the main reason your own code was so slow.
If s > 0 Then
Cells(1).Resize(r + 1, c) = a
Cells(c + 1).Resize(r) = u
Cells(1).Resize(r, c + 1).Sort Cells(c + 1), Header:=xlNo
Cells(1).Resize(s, c + 1).Delete xlUp
End If
End Sub
Bookmarks