Find a cell and delete a row

markman235

Board Regular
Joined
May 10, 2011
Messages
52
Hi everyone,

I have a sheet with multiple tabs, and they all contain data exept one that is the summary tab. The summary tab has a max formula to look across all tabs, and that works great.

My problem is that at the bottom of every data tab is a grand total, and this is what the formula is finding. So I would like to use a macro to find the words "Grand Total" and then delete the entire row.

Any help on this would be greatly appeciated.

Thank you!

Mark
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try:
Code:
Sub DeleteRow()
    Application.ScreenUpdating = False
    For Each ws In Sheets
        If ws.Name <> "Summary" Then
            ws.Rows(ws.Cells.Find("Grand Total", LookIn:=xlValues, lookat:=xlWhole).Row).EntireRow.Delete
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mumps,

Your code is much shorter than what I was going to post. However, you may want to make one small adjustment. If it does not find the phrase "Grand Total" on a sheet, it throws up an error and stops.
You might just want to add some error handling in the case in whcih it does not find that phrase on a page.
Just thinking about typical users, even if every page has Grand Totals, it is possible that the user may accidentally try to run the macro more than once.
 
Last edited:
Upvote 0
This should do it:
Code:
Sub DeleteRow()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, GT As Range
    For Each ws In Sheets
        If ws.Name <> "Summary" Then
            Set GT = ws.Cells.Find("Grand Total", LookIn:=xlValues, lookat:=xlWhole)
            If Not GT Is Nothing Then
                ws.Rows(GT.Row).EntireRow.Delete
            Else
                MsgBox ("'Grand Total' not found in sheet " & ws.Name)
            End If
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you!

I do have one minor problem, though. I get an error that says "Run-time error '91': Object variable or With block variable not set.

And it has the row ws.rows(ws.cells.find(.... highlighted in yellow.
 
Upvote 0
Have you tried the macro I posted in Post #4 (as @Joe4 suggested)?
 
Upvote 0
This should do it:
Code:
Sub DeleteRow()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, GT As Range
    For Each ws In Sheets
        If ws.Name <> "Summary" Then
            Set GT = ws.Cells.Find("Grand Total", LookIn:=xlValues, lookat:=xlWhole)
            If Not GT Is Nothing Then
                ws.Rows(GT.Row).EntireRow.Delete
            Else
                MsgBox ("'Grand Total' not found in sheet " & ws.Name)
            End If
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

This works great! Thank you!
 
Upvote 0
You are very welcome. Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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