I am fairly new to writing macros. I have been using the following code to delete rows from my worksheet when the text in column L meets the criteria defined in the code. I found this code online and tweaked it to work for my macro:
'This section of the code will delete the transfer to debenture loans from the file.
Dim X As Long
Dim Z As Long
Dim LastRow As Long
Dim FoundRowToDelete As Boolean
Dim OriginalCalculationMode As Long
Dim RowsToDelete As Range
Dim SearchItems() As String
Dim DataStartRow As Long
Dim SearchColumn As String
Dim SheetName As String
DataStartRow = 1
SearchColumn = "L"
SheetName = "detail"
SearchItems = Split("Tran to CPN,Settled,PIF", ",")
On Error GoTo Whoops
OriginalCalculationMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
With Worksheets(SheetName)
LastRow = .Cells(.Rows.Count, SearchColumn).End(xlUp).Row
For X = LastRow To DataStartRow Step -1
FoundRowToDelete = False
For Z = 0 To UBound(SearchItems)
If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) Then
FoundRowToDelete = True
Exit For
End If
Next
If FoundRowToDelete Then
If RowsToDelete Is Nothing Then
Set RowsToDelete = .Cells(X, SearchColumn)
Else
Set RowsToDelete = Union(RowsToDelete, .Cells(X, SearchColumn))
End If
If RowsToDelete.Areas.Count > 100 Then
RowsToDelete.EntireRow.Delete
Set RowsToDelete = Nothing
End If
End If
Next
End With
If Not RowsToDelete Is Nothing Then
RowsToDelete.EntireRow.Delete
End If
Whoops:
Application.Calculation = OriginalCalculationMode
Application.ScreenUpdating = True
This code has been running successfully - deleting the rows that I would expect it to delete.
Now we need to add another criteria for consideration before deletion. The row should be deleted if the coumn L contains the text Tran to CPN, Settled, or PIF AND the value in column AF is 0 (zero - not blank). I tried to do a nesting if:
With Worksheets(SheetName)
LastRow = .Cells(.Rows.Count, SearchColumn).End(xlUp).Row
For X = LastRow To DataStartRow Step -1
FoundRowToDelete = False
For Z = 0 To UBound(SearchItems)
If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) And .Cells(X, "AF").Value = "=0" Then
FoundRowToDelete = True
Exit For
End If
Now the rows are not deleting. Cell AF contains a formula. The results are truly 0 - not just rounding to 0. I even tested it by typing in a 0 in one of the cells rather than the formula and the macro still didn't delete the row. I'm not getting an error in VBA - it is running. It just isn't returning the desired results.
Please let me know if any additional information would be helpful. Thank you in advance for your help!!!
Kelly
Bookmarks