Excel delete number of rows if cells equals certain text

soldier2gud4me

New Member
Joined
Dec 29, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi there,

Im looking for a way to delete a total of 6 rows if a cell (C20) in the first of those 6 rows contains "#REF!". Here's what i have so far :

Sub Button7_Click()

Dim srchRng As Range

Worksheets("Summary").Activate

ActiveWindow.DisplayFormulas = False


Set srchRng = Range("C20:C300")

Dim c As Range
For Each c In srchRng
If c.Formula = "=#REF!" Then
ActiveWorkbook.Worksheets("Aug").Columns(2).SpecialCells(xlFormulas, xlErrors).EntireRow.Delete

Exit For
End If
Next

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here is a little macro that I think should do what you want.
VBA Code:
Sub Delete6Rows()

    Dim r As Long
   
    Application.ScreenUpdating = False
   
    Worksheets("Summary").Activate

'   Loop through rows backwards
    For r = 300 To 20 Step -1
'       Check formula in column C
        If Cells(r, "C").Formula = "=#REF!" Then
'           Delete row and 5 rows under it
            Rows(r & ":" & r + 5).Delete
        End If
    Next r
   
    Application.ScreenUpdating = True
   
    MsgBox "Macro complete!"
   
End Sub
 
Upvote 0
Solution
Here is a little macro that I think should do what you want.
VBA Code:
Sub Delete6Rows()

    Dim r As Long
  
    Application.ScreenUpdating = False
  
    Worksheets("Summary").Activate

'   Loop through rows backwards
    For r = 300 To 20 Step -1
'       Check formula in column C
        If Cells(r, "C").Formula = "=#REF!" Then
'           Delete row and 5 rows under it
            Rows(r & ":" & r + 5).Delete
        End If
    Next r
  
    Application.ScreenUpdating = True
  
    MsgBox "Macro complete!"
  
End Sub
It works !! Thank you very much. I was advised to try the function .Resize as well.
 
Upvote 0
You are welcome.
Glad I was able to help.

In Excel, there are often many different ways to accomplish a task.
So you could replace this line:
VBA Code:
            Rows(r & ":" & r + 5).Delete
with something like this:
VBA Code:
            Cells(r, "C").Resize(6, 1).EntireRow.Delete
and it should work the same.
 
Upvote 0
You are welcome.
Glad I was able to help.

In Excel, there are often many different ways to accomplish a task.
So you could replace this line:
VBA Code:
            Rows(r & ":" & r + 5).Delete
with something like this:
VBA Code:
            Cells(r, "C").Resize(6, 1).EntireRow.Delete
and it should work the same.
Thank you very much
 
Upvote 0

Forum statistics

Threads
1,223,649
Messages
6,173,580
Members
452,521
Latest member
bdough27

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