Deleting rows macro

RichJW

Board Regular
Joined
Jul 7, 2015
Messages
94
Hi,
I moved a working macro from one spreadsheet to another andit has now stopped working. In both spreadsheets, the data is in the same rowsand columns, with the exception of there now being one more column (L).
The data starts on row 5, with the headings on row 4. Theamount of rows differs each time I run the macro.
The macro previously found anything in column A with either “Apples”,“Bananas”, or “Plums” and deleted the entire row. Now it leaves the rows in thespreadsheet but throws up no errors.
Another user helped me with the macro, hence I am strugglingto decipher it.
The part of the macro not working is this…

lastrow =Cells(Rows.Count, 1).End(xlUp).Row
v1 =ActiveSheet.Range(Cells(1, 1), Cells(lastrow, 27)).Value2
bUnion = False
For i = 1 Tolastrow
If v1(i, 1) ="Apples" Or v1(i, 1) = "Bananas" Or v1(i, 1) = "Plums"Then
If bUnionThen
Set r1= Union(r1, Cells(i, 1))
Else
Set r1= Cells(i, 1)
bUnion= True
End If
End If
Next i

Thanks,
Rich
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
.
Can't say I understand the macro you have posted. Sorry.

However, this macro will review Column A from Row 5 to the last row, identify where any of the three terms exist and delete that entire row.

Code:
Option Explicit


Sub DeleteMyRows()


Dim Rng As Range, Cell As Range
Dim lr As Long, i As Long
lr = Range("A" & Rows.Count).End(xlUp).Row




 For i = 5 To lr
    
     If Range("A" & i) = "Apples" Or Range("A" & i) = "Bananas" Or Range("A" & i) = "Plums" Then
        Range("A" & i).EntireRow.Delete
     End If


Next i




End Sub
 
Upvote 0
However, this macro will review Column A from Row 5 to the last row, identify where any of the three terms exist and delete that entire row.
.. but not reliably. Try running it after entering "Apples" in A5:A8.
To remove rows individually like that you need to work from bottom to top.
Code:
For i = lr to 5 step -1


Rich
About how many rows of data might you have altogether?
 
Last edited:
Upvote 0
Thanks both, appreciate your help.

Peter, it will be between 10 and 1000.
Which line of text do I need to replace with the one Peter provided?

Rich
 
Upvote 0
Ah, got it.
The macro threw up an error on the second line of text, however once I removed the "i As Long" from the end it ran fine.
Thanks a lot for your help both of you.

Rich
 
Upvote 0
Rich
Can you post the code that you ended up using?
 
Last edited:
Upvote 0
Ah, got it.
The macro threw up an error on the second line of text, however once I removed the "i As Long" from the end it ran fine.
On looking back over the thread, I think that you may have been editing the wrong line. The one I was referring to in post 3 is the red one below.

Code:
Option Explicit

Sub DeleteMyRows()

Dim Rng As Range, Cell As Range
Dim lr As Long, i As Long
lr = Range("A" & Rows.Count).End(xlUp).Row


 [COLOR="#FF0000"][B]For i = 5 To lr[/B][/COLOR]
    
     If Range("A" & i) = "Apples" Or Range("A" & i) = "Bananas" Or Range("A" & i) = "Plums" Then
        Range("A" & i).EntireRow.Delete
     End If


Next i


End Sub
 
Upvote 0
Hi Peter, I used this...

Dim Rng As Range, Cell As Range
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row


For i = lr To 5 Step -1

If Range("A" & i) = "Apples" Or Range("A" & i) = "Bananas" Or Range("A" & i) = "Plums" Then
Range("A" & i).EntireRow.Delete
End If
 
Upvote 0
The macro you have just posted fails at Dim lr As Long, i As Long

It shows a compile error duplicate declaration in current scope
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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