I am trying to delete the duplicate rows from a excel using vba. The function checks for a value in a column, if it finds it then the row is deleted. I have written the following code, it works but alternate rows get deleted. One gets deleted one does not. The match is found in every iteration. Am I wrong anywhere?
Private Function DeletePreviousRecords(Program As String, targetSheet As String) As Boolean
Dim lastRow As Long, iCTR As Integer: iCTR = 2
Dim IsDone As Boolean
lastRow = GetLastRow(targetSheet)
Do
If Sheets(targetSheet).range("G" & CStr(iCTR)) = Program Then
MsgBox "Found Match at Row -" & CStr(iCTR)
Sheets(targetSheet).Rows(iCTR).Delete
IsDone = True
End If
iCTR = iCTR + 1
Loop While iCTR < lastRow
DeletePreviousRecords = IsDone
End Function
Private Function GetLastRow(sheetName As String) As Long
Dim lastRow As Long
'ActiveWorkbook.Sheets(targetSheet).Cells(Sheets(targetSheet).Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Sheets(sheetName)
lastRow = .Cells(Sheets(sheetName).Rows.Count, "C").End(xlUp).Row
End With
GetLastRow = lastRow + 1
End Function