How to delete 3 rows?

AdRock

Board Regular
Joined
Apr 1, 2011
Messages
128
Domenic was very kind enough to help with a problem posted here but I would like to expand this code slightly.

What I would like to do with this code is after the 30 rows have been inserted and a page break added, delete the row which has the search term "Test" and the next 2 rows.

This gets rid of the rows that i don't need.

Can anyone please help?

Code:
Option Explicit

Sub test()

    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim CurrAddress As String
    Dim SearchTerm As String

    SearchTerm = "Test"

    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                FoundCell.Resize(30).EntireRow.Insert
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress)
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
        
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Test on a dummy file as not sure this will quite work, looking at how your macro is looping for each value of "Test" but try (my additions in red):
Rich (BB code):
Option Explicit

Sub test()

    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim CurrAddress As String
    Dim SearchTerm As String

    SearchTerm = "Test"

    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                FoundCell.Resize(30).EntireRow.Insert
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress)
Rows(FoundCell.row & ":" & Found.Cell.row+2).delete
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
        
End Sub
 
Upvote 0
Thanks for your reply.

I tried what you suggested and I get this error message

"Unable to get FindNext property of the Range class"

and it points to

Code:
Set FoundCell = .FindNext(FoundCell)

Any ideas what is going wrong here?
 
Upvote 0
Try changing my line in red to:
Rich (BB code):
Rows(.FoundCell.row & ":" & .Found.Cell.row+2).delete


 
Upvote 0
Ok, taken something I seen in your existing code, try:
Rich (BB code):
Option Explicit

Sub test()

    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim CurrAddress As String
    Dim SearchTerm As String

    SearchTerm = "Test"

    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                FoundCell.Resize(30).EntireRow.Insert
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress)
FoundCell.Rezise(3).EntireRow.Delete
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
        
End Sub
The line in red is virtually identical to the line two above it so it should work now (famous last words)!
 
Upvote 0
I'd spelt resize wrong (rezise)
Rich (BB code):
Option Explicit

Sub test()

    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim PrevAddress As String
    Dim CurrAddress As String
    Dim SearchTerm As String

    SearchTerm = "Test"

    With Columns("A")
        Set FoundCell = .Find(SearchTerm, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not FoundCell Is Nothing Then
            FoundCell.Name = "FirstAddress"
            Do
                PrevAddress = FoundCell.Address
                FoundCell.Resize(30).EntireRow.Insert
                ActiveSheet.HPageBreaks.Add before:=Range(PrevAddress)
FoundCell.Resize(3).EntireRow.Delete
                Set FoundCell = .FindNext(FoundCell)
            Loop While FoundCell.Address <> Range("FirstAddress").Address
        Else
            MsgBox "No search term found...", vbExclamation
        End If
    End With
        
End Sub
 
Upvote 0
It deletes those 3 rows as it should but it's coming back with this error message again

<TABLE border=0 cellSpacing=0 cellPadding=6 width="100%"><TBODY><TR><TD style="BORDER-BOTTOM: 1px inset; BORDER-LEFT: 1px inset; BORDER-TOP: 1px inset; BORDER-RIGHT: 1px inset" class=alt2>"Unable to get FindNext property of the Range class" </TD></TR></TBODY></TABLE>
<TABLE border=0 cellSpacing=0 cellPadding=6 width="100%"><TBODY><TR><TD style="BORDER-BOTTOM: 1px inset; BORDER-LEFT: 1px inset; BORDER-TOP: 1px inset; BORDER-RIGHT: 1px inset" class=alt2>"Unable to get FindNext property of the Range class" </TD></TR></TBODY></TABLE>
<TABLE border=0 cellSpacing=0 cellPadding=6 width="100%"><TBODY><TR><TD style="BORDER-BOTTOM: 1px inset; BORDER-LEFT: 1px inset; BORDER-TOP: 1px inset; BORDER-RIGHT: 1px inset" class=alt2>"Unable to get FindNext property of the Range class" </TD></TR></TBODY></TABLE>
"Unable to get FindNext property of the Range class"
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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