Sub AgedDebtor_01()
Dim shtRaw As Worksheet
Dim ShtOver2day As Worksheet
Dim ShtTemp As Worksheet
Dim lDstRowNum As Long
Dim Rng As Range, fm As String, i As Long, r As Long, a, v
Set shtRaw = ThisWorkbook.Sheets("Raw")
Set ShtOver2day = ThisWorkbook.Sheets("Workings")
' Turn off events triggering and screen updating
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Clear existing data
With ShtOver2day
' Limit the range to be cleared by UsedRange
Intersect(.UsedRange, .Range("A6:D" & Rows.Count)).Clear
End With
'Add Temp sheet to copy Data
Set ShtTemp = Sheets.Add
With shtRaw
If .FilterMode Then .ShowAllData
lDstRowNum = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A1:M" & lDstRowNum).Copy ShtTemp.Range("A1")
End With
' Do main processing
With ShtTemp
' Prepare sheet to processing
With .UsedRange
.EntireColumn.AutoFit
.RemoveSubtotal
' Copy cells as values to exclude an extra recalculations
.Copy
.PasteSpecial xlPasteValues
.Cells(1.1).Select
Application.CutCopyMode = False
End With
' Delete some columns
.Range("A:B,D:F").Delete
' Provide fast deleting of the rows with "Receipt" in C column
a = Intersect(.UsedRange, .Columns("C")).Value
For r = 1 To UBound(a)
v = a(r, 1)
a(r, 1) = Empty
If VarType(v) = vbString Then
If StrComp(v, "Receipt", vbTextCompare) = 0 Then
i = i + 1
a(r, 1) = 1
End If
End If
Next
If i > 0 Then
With .UsedRange
.Columns(1).Offset(, .Columns.Count).Value = a
End With
With .UsedRange
.Sort .Cells(1, .Columns.Count), xlAscending, Header:=xlNo
.Rows(1).Resize(i).Delete
End With
End If
' Do some actions as it were in original code
lDstRowNum = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A5:A" & lDstRowNum).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("K5"), Unique:=True
.Range("I5:I" & lDstRowNum).FormulaR1C1 = "=SUM(RC[-4]:RC[-1])"
' Put fоrmula "=SUMIF(Col_A,Cell_K,Col_I)" into Col_L range (refer to post #2)
Set Rng = .Range("A5", .Cells(.Rows.Count, "A").End(xlUp))
fm = "=SUMIF(" & Rng.Address & ",K5," & Rng.Columns("I").Address & ")"
Rng.Columns("L").Formula = fm
' Put an array fоrmula into M column
fm = "=ISNUMBER(MATCH(RC[-2],rngClientCode,0))+0"
Rng.Columns("M").FormulaArray = fm
' Replace entered formulas by values
With .UsedRange
.Copy
.PasteSpecial xlPasteValues
End With
End With ' <- ShtTemp
' Reset events and screen updating
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub