Solved

Excel VB To Delete Rows If Specific Text Is Found

Posted on 2013-01-10
5
385 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

839 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