Solved

Excel VB To Delete Rows If Specific Text Is Found

Posted on 2013-01-10
5
360 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Brian - No worries - I figured out what was happening.  Thank you for assisting on this one!
0
 
LVL 26

Expert Comment

by:redmondb
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query output to Excel producing error 7 34
Excel 3 21
Moving Range Names/Formulas over to new WB 21 12
Macro to delete column 3 0
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

744 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

13 Experts available now in Live!

Get 1:1 Help Now