select and remove multiple rows based on content

tmd63

New Member
Joined
Feb 21, 2014
Messages
40
Office Version
  1. 2013
  2. 2003 or older
Platform
  1. Windows
I have a simple request, but it is difficult to code.
I am using Excel 2010 and I have a single sheet spreadsheet.
What I need to do is find every instance of a piece of text ("Tape") in a column on the spreadsheet and delete the row that this text appears in. The text is on it's own (so is not part of a large piece of text) but may appear once only or 1,000 of times in a spreadsheet.
Can anyone help with some macro code that can perform this action please?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try the below (Which checked column B and ignores the case of the text)

Code:
Sub DeleteRows()
Dim LRow As Long


Application.ScreenUpdating = False
    
    For LRow = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("B" & LRow).Value = "" Or UCase(Range("B" & LRow)) = "TAPE" Then
            Rows(LRow).EntireRow.Delete
        End If
    Next LRow


Application.ScreenUpdating = True
End Sub


Adapted from the below link



http://www.mrexcel.com/forum/excel-...applications-delete-row-based-cell-value.html
 
Upvote 0
Thank you.

I had to adapt it slightly as it did not delete the rows correctly.


If Range("B" & LRow).Value = "" Or UCase(Range("B" & LRow)) = "TAPE" Then
deleted blank rows also!

If Range("B" & LRow).Value = "Tape" Or UCase(Range("B" & LRow)) = "Tape" Then

This is my new code.
</pre>
 
Upvote 0
Then you will only need

Code:
If UCase(Range("B" & LRow)) = "TAPE" Then

Thank you.

I had to adapt it slightly as it did not delete the rows correctly.


If Range("B" & LRow).Value = "" Or UCase(Range("B" & LRow)) = "TAPE" Then
deleted blank rows also!

If Range("B" & LRow).Value = "Tape" Or UCase(Range("B" & LRow)) = "Tape" Then

This is my new code.
 
Upvote 0
tmd63,

Below is a solution without looping.

Sample raw data:


Excel 2007
B
1Title B
2Tape
3tape
44
5Tape
66
7TAPE
88
9tape
10tape
1110
12
Sheet1


After the macro:


Excel 2007
B
1Title B
24
36
48
510
6
7
8
9
10
11
12
Sheet1


Sample raw data:


Excel 2007
B
1Title B
2Tape
3tape
4TAPE
5Tape
6TAPE
7TAPE
8tape
9tape
10tape
11TAPE
12
Sheet1


After the macro:


Excel 2007
B
1Title B
2
3
4
5
6
7
8
9
10
11
12
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Option Compare Text
Sub DeleteColB_rows_tape()
' hiker95, 02/21/2014, ME759272
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("B2:B" & lr) = Evaluate("IF(B2:B" & lr & "<>""tape"",B2:B" & lr & ","""")")
On Error Resume Next
Range("B1:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the DeleteColB_rows_tape macro.
 
Upvote 0
If the values in Column B are all constants (that is, Column B has no formulas in it), then here is another non-looping macro for you to try...

Code:
Sub DeleteRowsWithTapeInColumnB()
  Columns("B").Replace "Tape", "#N/A", xlWhole, , False
  On Error Resume Next
  Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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