Deleting previous and next row

weep

New Member
Joined
Aug 17, 2011
Messages
7
Hey,

It would be awesome if someone could help me with following:
I need to find a row with some specifik text and then delete it and previous and next row. I have found a script that deletes this one specific row, but how do I remove the rest?

Code:
Sub Loop_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

            'We check the values in the A column in this example
            With .Cells(Lrow, "R")

                If Not IsError(.Value) Then

                    If .Value = "V90 Verksamhetsankn. utr-" Then .EntireRow.Delete
                    'This will delete each row with the Value "ron"
                    'in Column A, case sensitive.

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub
thanks in advance guys!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
may be this will help
Code:
Sub Test()
Dim LR As Long, i As Long
LR = Range("R" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
If Range("R" & i).Value = "V90 Verksamhetsankn. utr-" Then
    Range("R" & i).Offset(-1).Resize(3).EntireRow.Delete
End If
Next i
End Sub
 
Upvote 0
Hello and welcome.

Change this line...
Code:
If .Value = "V90 Verksamhetsankn. utr-" Then .EntireRow.Delete

To this...
Code:
If .Value = "V90 Verksamhetsankn. utr-" Then [COLOR="Red"].Offset(-1).Resize(3)[/COLOR].EntireRow.Delete
 
Upvote 0
I'm using the same macro, but I need to keep the first row, I don't want this one instance deleted even though it has the criteria. How do I offset the first row?

Firstrow = .UsedRange.Cells(1).Row ???


Duh found it .UsedRange.Cells(1).Row + 1
 
Last edited:
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