VBA to delete rows from table that do not contain specific text

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I am looking for a VBA code to delete rows from a table that do not contain an "M". The information is in the fourth column (Column D) and has a header of "Category". There are five options for what will be in Column D; "M", "S", "E", "L", "O". I want to delete any row that does not contain "M". I have a solution that runs a loop but there could be thousands of lines and this loop gets bogged down.

Any Suggestions?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Different ways but as it depends on the worksheet design so I can't help without an attachment​
but you can already see the many threads about this common subject …​
 
Upvote 0
I tried looking but could not find an appropriate thread, I must have poor search skills

Here is an example of the table
Acct IDSection #Section DescriptionCategoryCost CodeCost Code DescriptionAccount CodeAccount Code DescriptionQuantityUOM
71D0001
9500​
Acoustical CeilingsMFASTENERSFastenersMT114FASTENERS
666.73​
EA
71D0001
9500​
Acoustical CeilingsMFASTENERSFastenersMT114FASTENERS
787.96​
EA
71D0001
9500​
Acoustical CeilingsLACHWAC - Grid Hanger Wires
12420​
AC - Hanger Wire
606.12​
EA
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
528.22​
EA
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
294.25​
LF
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
4576.91​
LF
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
544.06​
LF
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
184.82​
SF
71D0001
9500​
Acoustical CeilingsMACAcoustical CeilingsMT100ACOUSTICAL CEILING
9513.09​
SF
71D0001
9500​
Acoustical CeilingsLACANGLESHADOWAC - Angle Shadow
12422​
AC - Wall Angle
2289.23​
LF
71D0001
9500​
Acoustical CeilingsLACGRID24X24AC - Grid 24" x 24"
12401​
AC - Grid
2816.33​
SF
71D0001
9500​
Acoustical CeilingsLACACCESSPERIMETERTRIMAC Accessories - Perimeter Trim
12436​
AC - Edge Trim (Axiom or Compasso)
529.09​
LF
71D0001
9500​
Acoustical CeilingsLACGRIDMISCEAAC - Grid Misc EA
12490​
AC - Misc EA
111​
EA
71D0001
9500​
Acoustical CeilingsLACGRIDMISCLFAC - Grid Misc LF
12494​
AC - Misc LF
231.15​
LF

I only want to be left with "M" in the "Category" column
 
Upvote 0
So difficult to see without a real attachment : is it a true Excel table or just a range of cells ?​
In case of an Excel table you can use a files hosting website and link a workbook saved in binary mode (.xlsb) …​
 
Upvote 0
So difficult to see without a real attachment : is it a true Excel table or just a range of cells ?​
In case of an Excel table you can use a files hosting website and link a workbook saved in binary mode (.xlsb) …​
I have no idea how to do that
 
Upvote 0
And the answer to the question is …❓
 
Upvote 0
How about
VBA Code:
Sub gberg()
   With ActiveSheet
      .Range("A1:J1").AutoFilter 4, "<>M"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Solution
Hi Fluff,​
as the OP has stated for 'thousands of rows' I hope there are not to much non contiguous rows to delete or like this it may need a while …​
 
Upvote 0
It will still be faster than looping & deleting rows individually.
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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