Solved

Excel VB To Delete Rows If Specific Text Is Found

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

695 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