Sub DeleteZeroTotals()
Set aaa = ActiveSheet
lEndRow = aaa.Range("E" & aaa.Rows.Count).End(xlUp).Row
On Error Resume Next
StartAgain:
bolFound = False
For Each rng In aaa.Range("d2:d" & lEndRow).SpecialCells(xlCellTypeBlanks)
If rng.Offset(0, 1) > -1 And rng.Offset(0, 1) < 1 Then
If rng.Row > 3 Then
If rng.End(xlUp).End(xlUp).Row > 1 Then
aaa.Range(rng, rng.End(xlUp).End(xlUp)).EntireRow.Delete
Else
aaa.Range(rng, rng.End(xlUp).End(xlUp).Offset(1, 0)).EntireRow.Delete
End If
bolFound = True
Else
aaa.Range(rng, rng.End(xlUp)).EntireRow.Delete
bolFound = True
End If
End If
Next rng
lEndRow = aaa.Range("E" & aaa.Rows.Count).End(xlUp).Row
If bolFound = True Then GoTo StartAgain
End Sub
FranciscoDear Excel Community:
Has anyone developed any VBA code to delete rows in a subtotalled list?
The subtotal line and the associated rows should be deleted when subtotal equals zero.
Thanks in advance
Francisco
Sub test()
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
Set rng = Cells(r, "A")
cf = rng.Formula
cv = rng.Value
If InStr(cf, "SUBTOTAL") And cv = 0 Then
addr = Mid(cf, InStr(cf, ",") + 1, Len(cf))
addr = Left(addr, InStr(addr, ")") - 1)
Union(Range(addr), rng).EntireRow.Delete
End If
Next
End Sub
Francisco
Welcome to the MrExcel board!
Can you give us some more detail about your subtotalled data?
1. Does it start in cell A1? If not, where?
2. Does it have a fixed number of columns? How many?
3. Which of these columns do we have to check for zero subtotals?
This is not particularly sophisiticated, so I'd try it on a sample of data first. It assumes the values and subtotals are in column A and the whole row should be deleted. It also assumes the first row is a header.
Code:Sub test() lr = Cells(Rows.Count, "A").End(xlUp).Row For r = lr To 2 Step -1 Set rng = Cells(r, "A") cf = rng.Formula cv = rng.Value If InStr(cf, "SUBTOTAL") And cv = 0 Then addr = Mid(cf, InStr(cf, ",") + 1, Len(cf)) addr = Left(addr, InStr(addr, ")") - 1) Union(Range(addr), rng).EntireRow.Delete End If Next End Sub
HTH