delete rows contains TOTAL word across sheets

Mussala

Board Regular
Joined
Sep 28, 2022
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
Hi
I have rows some of them contains TOTAL word in column A for just two sheets(sh, mn) . I want deleting the whole rows contain TOTAL word in column A for two sheets.
thanks
 
Another option
VBA Code:
Sub Mussala2()
Dim ws As Worksheet
For Each ws In Worksheets
Select Case ws.Name
    Case "sh", "mn"
     With ws.Range("A1", Cells(Rows.Count, "A").End(xlUp))
        .Replace "TOTAL", "#N/A", xlWhole, , False, , False, False
        ws.Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    End With
End Select
Next ws
End Sub
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There is a much faster method (leave it with me) but how much bigger is the data?
 
Upvote 0
VBA Code:
Sub row_delete2()
        Dim lr As Long: lr = Range("A" & Rows.Count).End(xlUp).Row
        
        
        Range("B2:B" & lr).Formula = "=if(A2=""total"",1,"""")"
        Range("B2:b" & lr) = Range("B2:b" & lr).Value
        
        Range("A1").CurrentRegion.Sort key1:=Range("B1"), order1:=xlDescending, Header:=xlYes
        Range("b2:b" & Range("B" & Rows.Count).End(xlUp).Row).EntireRow.Delete
        
        
End Sub
 
Upvote 0
Try the following - tested on 2 sheets of 25K rows around 0.25 seconds.
VBA Code:
Option Explicit
Sub Mussala_V2()
    Application.Calculation = xlManual
    Application.ScreenUpdating = False
    Dim ws As Worksheet, LRow As Long, LCol As Long, i As Long, a, b
    
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
            Case "sh", "mn"
            LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
            LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
            a = Range(ws.Cells(2, 1), ws.Cells(LRow, 1))
            ReDim b(1 To UBound(a), 1 To 1)
            For i = 1 To UBound(a)
                If a(i, 1) = "TOTAL" Then b(i, 1) = 1
            Next i
            ws.Cells(2, LCol).Resize(UBound(a)) = b
            i = WorksheetFunction.Sum(ws.Columns(LCol))
            If i > 0 Then
                ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
                order1:=xlAscending, Header:=xlNo
                ws.Cells(2, LCol).Resize(i).EntireRow.Delete
            End If
        End Select
    Next ws
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
@Michael M
thanks , it gives error method of range object_workseet failed
VBA Code:
With ws.Range("A1", Cells(Rows.Count, "A").End(xlUp))
 
Upvote 0
@kevin9999
this is really fast .(y)
just I have new request sorry !
may you re-autonumbering in column A : 1,2.3... after delete TOTAL row for each sheet ,please ?
 
Upvote 0
re-autonumbering in column A : 1,2.3.
See if the following gives you what you want:

VBA Code:
Option Explicit
Sub Mussala_V3()
    Application.Calculation = xlManual
    Application.ScreenUpdating = False
    Dim ws As Worksheet, LRow As Long, LCol As Long, i As Long, a, b
    
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
            Case "sh", "mn"
            LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
            LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
            a = Range(ws.Cells(2, 1), ws.Cells(LRow, 1))
            ReDim b(1 To UBound(a), 1 To 1)
            For i = 1 To UBound(a)
                If a(i, 1) = "TOTAL" Then b(i, 1) = 1
            Next i
            ws.Cells(2, LCol).Resize(UBound(a)) = b
            i = WorksheetFunction.Sum(ws.Columns(LCol))
            If i > 0 Then
                ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
                order1:=xlAscending, Header:=xlNo
                ws.Cells(2, LCol).Resize(i).EntireRow.Delete
            End If
            LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
            With ws.Range("A2:A" & LRow)
                .Value = Evaluate("ROW(" & .Address & ")-1")
            End With
        End Select
    Next ws
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,167
Messages
6,123,401
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top