the below code used to:
Concatenate the values on a specific column "N" depend on the value of column "A" then delete the remaining rows.
It works, but with range of 30k rows the macro takes a long time to finish (14 seconds) on powerful PC.
Edit:
the bottle neck is on this line .SpecialCells(xlCellTypeConstants).EntireRow.Delete
(it takes 13.5 seconds) from the overall code time ( 14 seconds).
I tried to replace it with VBA AutoFilter
, but the same issue.
This is updated screenshot: of current values and the current result,
My goal is to do all processing on arrays or dictionary to achieve the fastest speed.
I have office 2016 on my work.
Option Explicit
Option Compare Text
Sub Concatenate_column_N_values_Delete_remaining_Rows()
Dim t: t = Timer
Const sep As String = vbLf
Dim arrKeys, arrVals, arrFlags, rngRows As Range, key, currKey, s As String
Dim ub As Long, n As Long, ws As Worksheet, rngVals As Range, i As Long
Set ws = ActiveSheet
Set rngRows = ws.Range("A2:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row) 'Column Contains WO
Set rngVals = rngRows.EntireRow.Columns("N") 'Column contains pure string
Application.ScreenUpdating = False
arrKeys = rngRows.Value2
ub = UBound(arrKeys, 1)
arrVals = rngVals.Value2
ReDim arrFlags(1 To UBound(arrKeys, 1), 1 To 1)
currKey = Chr(0) 'non-existing key...
For i = ub To 1 Step -1 'looping from bottom up
key = arrKeys(i, 1) 'this row's key
If key <> currKey Then 'different key from row below?
If i < ub Then arrVals(i + 1, 1) = s 'populate the collected info for any previous key
s = arrVals(i, 1) 'collect this row's "N" value
currKey = key 'set as current key
Else
If i < ub Then
arrFlags(i + 1, 1) = "x" 'flag for deletion
n = n + 1
End If
s = arrVals(i, 1) & sep & s 'concatenate the "N" value
End If
Next i
arrVals(1, 1) = s 'populate the last (first) row...
rngVals.Value = arrVals 'drop the concatenated values
If n > 0 Then 'any rows to delete?
With rngRows.Offset(0, 100) 'use any empty column
.Value = arrFlags
.SpecialCells(xlCellTypeConstants).EntireRow.Delete
End With
End If
Application.ScreenUpdating = True
Debug.Print "Concatenate_column_N_values_Delete_remaining_Rows, in " & Round(Timer - t, 2) & " sec"
End Sub