Solved

Excel VB To Delete Rows If Specific Text Is Found

Posted on 2013-01-10
5
371 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
  • 3
  • 2
5 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now