Delete multiple rows having specific texts

mshahbaz

New Member
Joined
Feb 13, 2017
Messages
36
Hi,

I want to delete all rows having anyone of below texts.

1. Printed
2. Capsule
3. Tablet

Please advise quick techniques.

Shahbaz

For example:

Column 2

Printed aluminium sheet
Sheets printed
Acetaminophen tablets
Vit. E capsules
Capsules of vitamins
 
What if i need to apply this code on "Column C" instead of "Column B"?

I made following changes in your vba code, but it is not working on "Column C".

a = Range("C1", Range("C" & Rows.Count).End(xlUp)).Value



Shahbaz
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What if i need to apply this code on "Column C" instead of "Column B"?

I made following changes in your vba code, but it is not working on "Column C".

a = Range("C1", Range("C" & Rows.Count).End(xlUp)).Value



Shahbaz


It would be much easier if i can apply this formula on Column B & C simultaneously with one vba code.
 
Upvote 0
Hi Peter,

It worked great!

Thanks a lot.

Shahbaz
You are welcome, thanks for the follow-up.



I made following changes in your vba code, but it is not working on "Column C".

a = Range("C1", Range("C" & Rows.Count).End(xlUp)).Value
That is exactly the correct change to make to work on column C. I don't know why it didn't work for you. I tested and it worked for me.



It would be much easier if i can apply this formula on Column B & C simultaneously with one vba code.
If the bottom row of data in col B is on the same row as the bottom row of data in col C and your requirement is to delete rows that have any of your text strings in either column, then just 2 changes should be required.

That same line as above would change to
Rich (BB code):
a = Range("B1", Range("C" & Rows.Count).End(xlUp)).Value

and the line 3 lines below would become
Rich (BB code):
s = LCase(a(i, 1) & "|" & a(i, 2))

If columns B & C could have data ending on different rows, or if you only want to delete a row if one of your texts appears in both columns, then a few more changes would be needed. Post back with more details if that is required in your circumstances.
 
Upvote 0
You are welcome, thanks for the follow-up.



That is exactly the correct change to make to work on column C. I don't know why it didn't work for you. I tested and it worked for me.



If the bottom row of data in col B is on the same row as the bottom row of data in col C and your requirement is to delete rows that have any of your text strings in either column, then just 2 changes should be required.

That same line as above would change to
Rich (BB code):
a = Range("B1", Range("C" & Rows.Count).End(xlUp)).Value

and the line 3 lines below would become
Rich (BB code):
s = LCase(a(i, 1) & "|" & a(i, 2))

If columns B & C could have data ending on different rows, or if you only want to delete a row if one of your texts appears in both columns, then a few more changes would be needed. Post back with more details if that is required in your circumstances.


Great!

Just one more change required: On other sheets i need to enter this code for Column C and Column E.



Regards
 
Upvote 0
On other sheets i need to enter this code for Column C and Column E.
Rich (BB code):
a = Range("C1", Range("E" & Rows.Count).End(xlUp)).Value
and
Rich (BB code):
s = LCase(a(i, 1) & "|" & a(i, 3))
 
Upvote 0
Rich (BB code):
a = Range("C1", Range("E" & Rows.Count).End(xlUp)).Value
and
Rich (BB code):
s = LCase(a(i, 1) & "|" & a(i, 3))

Hi Peter

One more addition needed.

I have to delete all rows having values <25 from Column G only.

Is there any possibility to make an addition in this code without complication, or
should it be a separate macro for it?
 
Upvote 0
Sorry, I've been away for a few days.

One more addition needed.

I have to delete all rows having values <25 from Column G only.
This addition is not clear to me.

a) Do you want rows deleted if they have certain texts in other columns and <25 in column G, or

b) Do you want rows deleted if they have certain text in other columns or <25 in column G, or

c) Is this question unrelated to the previous questions, or

d) Something else?

If a) or b), which columns? There have been a few different requirements during the thread.


In fact, why not give some dummy sample data and explain which rows should be deleted and why?
 
Upvote 0
mshahbaz,

If I understand you correctly, then here is another macro solution for you to consider that does not use looping thru the rows in column B.

Sample raw data:


Excel 2007
B
1Printed aluminium sheet
2Sheets printed
3Acetaminophen tablets
4Vit. E capsules
5Capsules of vitamins
6printed
7capsule
8tablet
9Please advise quick techniques.
10Shahbaz
11hiker95
12
Sheet1


And, after the macro:


Excel 2007
B
1Please advise quick techniques.
2Shahbaz
3hiker95
4
5
6
7
8
9
10
11
12
Sheet1



Code:
Sub mshahbaz()
' hiker95, 02/19/2017, ME990911
Dim Addr As String
Addr = "B1:B" & Cells(Rows.Count, "B").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(ISNUMBER(SEARCH(""printed"",@)),""#N/A"",@)", "@", Addr))
Range(Addr) = Evaluate(Replace("IF(ISNUMBER(SEARCH(""capsule"",@)),""#N/A"",@)", "@", Addr))
Range(Addr) = Evaluate(Replace("IF(ISNUMBER(SEARCH(""tablet"",@)),""#N/A"",@)", "@", Addr))
On Error GoTo NoDeletes
Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoDeletes:
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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