I don't know much VBA besides googling, copying code, trying (and mostly failing) and am having trouble doing the following.
I want to delete the entire row if the value in column AF is < 60 and then delete the entire row if the value in column AG < 90 .
I am able to do the first part with the following:
'get last row in column AF
Last = Cells(Rows.Count, "AF").End(xlUp).Row
For i = Last To 1 Step -1
'if cell value is less than 60
If (Cells(i, "AF").Value) < 60 Then
'delete entire row
Cells(i, "AF").EntireRow.Delete
End If
Next i
But deleting cells in AG <90 fails with the following code:
'get last row in column AG
Last = Cells(Rows.Count, "AG").End(xlUp).Row
For i = Last To 1 Step -1
'if cell value is less than 90
If (Cells(i, "AG").Value) < 90 Then
'delete entire row
Cells(i, "AG").EntireRow.Delete
End If
Next i
The first part works (delete cells in AF <60) but the second part does not work and I get the following error: "Run-time error '13' Type mismatch.
I assume it is a simple fix that I am struggling with because I don't really know VBA. Any help would be appreciated.
You probably should combine both rules at the same time and also delete after you loop through everything. this will be significantly more effecient. See below (untested).
Sub DeleteSomeRows()
Dim killRange As Range
Dim the90Column As Long, the60Column As Long, i As Long, Last As Long
the90Column = Range("AG1").Column
the60Column = Range("AF1").Column
Last = Cells(Rows.Count, "AG").End(xlUp).Row
For i = Last To 1 Step -1
'if cell value is less than 90
If Cells(i, the90Column).Value < 90 Or Cells(i, the60Column).Value < 60 Then
If killRange Is Nothing Then
Set killRange = Cells(i, 1).EntireRow
Else
Set killRange = Union(killRange, Cells(i, 1).EntireRow)
End If
End If
Next i
If Not killRange Is Nothing Then
killRange.Delete
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.