I am grappling with a bizarre Match
malfunction which is difficult to understand and seek explanations for possible causes.
Several authors have helped compile the following code:
Dim t1s As Long: t1s = 1
Dim t2s As Long: t2s = 6
Dim t1l As Long: t1l = 4 - 1
Dim t2l As Long: t2l = 5 - 1
Dim r As Long
Dim rMatch As Long
With wsR
For r = wsR.Cells(.Rows.Count, t1s).End(xlUp).Row To 2 Step -1
If Application.WorksheetFunction.CountIf(.Columns(t2s), wsR.Cells(r, t1s).Value) > 0 Then
rMatch = Application.WorksheetFunction.Match(.Cells(r, t1s).Value, .Columns(t2s), 0)
.Range(.Cells(r, t2s), .Cells(r, t2s + t2l)).Insert shift:=xlDown
.Range(.Cells(rMatch, t2s), .Cells(rMatch, t2s + t2l)).Cut
.Select
.Range(.Cells(r, t2s), .Cells(r, t2s + t2l)).Select
.Paste
.Range(.Cells(rMatch, t2s), .Cells(rMatch, t2s + t2l)).Delete
End If
Next r
End With
The code runs beautifully (match, insert, cut, paste, delete, repeat) until halfway through a dataset, at which point it then inexplicably "breaks", returning unmatched cells and shifting rows beneath the deleted row, up. This action also scrambles the prior correct results above.
I have debugged line by line and can find no clue apart from the fact it coincides with date changes in a corresponding column. Excel does have some oddities but I am convinced there is a simple explanation. Whoever knows a good place to start, please do not simply provide a solution - it's really important I learn the skill of debugging.
Many thanks.