i have a large spreadsheet with certain rows i need to delete if they have certain strings of text. i need coding to delete rows only if they contain any of the various certain strings of text as a whole. i found code that deletes rows if any single word is found, but i need it to find only exact words together (e.g. county of residence, - not just if any of those words are found elsewhere like when "of" is found in "zip code of residence"
so: i need to delete rows that contain "county or residence" or "age of respondent" or "zip code of employment"
I also need it to search 2 different columns rather than just one.
(i think macros would be the easiest solution??)
any help would be appreciated! thanks!
Maybe something like this?Option Compare Text Sub DeleteRowsMatchingText() Dim rFoundText As Range Dim rLookRange As Range Dim vText() As Variant Dim vItem As Variant Dim lCalc As Long With Application lCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual End With vText = Array("County of Residence", "Age of Respondent", "Zip Code of Employment") Set rLookRange = Range("A:A,C:C") 'Change A:A and C:C to your search columns For Each vItem In vText Do Set rFoundText = rLookRange.Find(vItem, LookIn:=xlValues, lookat:=xlWhole) If Not rFoundText Is Nothing Then rFoundText.EntireRow.Delete End If Loop Until rFoundText Is Nothing Next Set rLookRange = Nothing Set rFoundText = Nothing With Application .ScreenUpdating = True .Calculation = lCalc End With End Sub
Last edited by gjlindn; 09-14-2011 at 12:30 AM. Reason: typo
-GregIf this is helpful, pls click Star icon in lower left corner
How about this?Option Compare Text Option Explicit Sub DeleteRowsMatchingText() Dim rFoundText As Range Dim rLookRange As Range Dim vText() As Variant Dim vItem As Variant Dim lCalc As Long With Application lCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual End With vText = Array("County of Residence", "Age of Respondent", "Zip Code of Employment") Set rLookRange = Range("A:A,C:C") 'Change A:A and C:C to your search columns For Each vItem In vText Do Set rFoundText = rLookRange.Find(vItem, LookIn:=xlValues, lookat:=xlWhole) If Not rFoundText Is Nothing Then rFoundText.EntireRow.Delete End If Loop Until rFoundText Is Nothing Next Set rLookRange = Nothing Set rFoundText = Nothing With Application .ScreenUpdating = True .Calculation = lCalc End With End Sub
-GregIf this is helpful, pls click Star icon in lower left corner
If you need to do this an a regular bases then a macor is a good way to go - It also depends on how many rows need to be checked.
It may alo possible to use formulas, manual sorting and manula deleting
The early part of threadhas various methods of using a macro to search for a whole cell content in a single columnHTML Code:http://www.excelforum.com/excel-programming/792012-remove-multiple-rows.html
These various methods can be modified to search a 2nd column.
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
works! thanks!!
Glad I could helpIf you haven't already, you should also check out the link mudraker posted. It's great to be able to use vba to solve an issue, but with a little bit of formula and native Excel function knowledge you can decide for yourself if vba is appropriate. Have a great day!
-GregIf this is helpful, pls click Star icon in lower left corner
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks