?
Solved

Excel VB To Delete Rows If Specific Text Is Found

Posted on 2013-01-10
5
Medium Priority
?
396 Views
Last Modified: 2013-01-10
Hello,

Can someone provide a code that looks at sheet2, starting at row 2, and if the text:  Browser_type is found, the entire row is deleted?  This will need to loop to the end of the worksheet until no data is found.

Thanks!
0
Comment
Question by:Escanaba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 2000 total points
ID: 38764115
Hi, Escanaba.

Please see attached. A few points...
(1) The search is case-insensitive.
(2) The search will find entries such as "XYZBrowser_typeABC".
(3) If you have a small number of "Browser_type" entries in a large file then this could be more efficient. Let me know!

The code is...
Option Explicit

Sub Delete_Rows()
Dim xCell     As Range
Dim xLast_Row As Long
Dim i         As Long

Sheets("To Be Deleted").Activate

If ActiveSheet.UsedRange.Rows.Count > 0 Then xLast_Row = [A1].SpecialCells(xlLastCell).Row
If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Application.ScreenUpdating = False
    
    For i = xLast_Row To 2 Step -1
        If Not Range("A" & i).EntireRow.Find(What:="Browser_type", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) Is Nothing Then
            Range("A" & i).EntireRow.Delete
        End If
    Next
    
If ActiveSheet.UsedRange.Rows.Count < 0 Then Debug.Print "?!"
    
Application.ScreenUpdating = True

End Sub

Open in new window

Regards,
Brian.Delete-Rows.xls
0
 
LVL 1

Author Comment

by:Escanaba
ID: 38764392
Hi Brian -  Your code appears to work but it takes a very long time to run through 150,000 rows of data.  Any recommendations on speeding it up?
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38764424
Escanaba,

A few questions, please...
(1) Roughly how long?
(2) Any formulas on the sheet? If so, changing Calculation to manual would definitely help!
(3) Roughly how many "Browser_type" entries do you have?

Thanks,
Brian.
0
 
LVL 1

Author Closing Comment

by:Escanaba
ID: 38764854
Brian - No worries - I figured out what was happening.  Thank you for assisting on this one!
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38764919
Thanks, Escanaba.

I figured out what was happening.
Are there any learnings for others who find this question? Edit: And me!

Thanks,
Brian.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question