Delete rows that contains certain text - fastest way

saulodefaria

New Member
Joined
Jul 8, 2014
Messages
3
Hi guys!

Here's my problem: I have some huge txt files (~700k lines) with csv data from stock prices. One of the columns is the name of the company. So I want to get only the lines of certain companies. I actually already have a solution, but it is taking too much time. I think it may take hours to filter just one txt file, but I have lots of them...

Here's the code I'm using now:

Code:
Sub delete()


Dim dontDelete
dontDelete = Array("APPLE", "MICROSOFT", "GOOGLE")


Dim i As Long, j As Long


Dim isThere As Boolean


For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    For j = LBound(dontDelete) To UBound(dontDelete)
        If InStr(1, Range("A" & i), dontDelete(j)) Then
            isThere = True
        End If
    Next j
    If Not isThere Then
        Range("A" & i).delete shift:=xlUp
    End If
    isThere = False
Next i


End Sub

It does the job. But I was hoping I could get some ideas to do it faster.

Thanks!
 
saulodefaria,

actually there are like 20 companies that I want to keep (not only 3), so is there a smarter way to put all the company names as constraint (maybe in an array) instead of doing ---InStr(oa(i, 1), "companyX") Or--- to all companies?

It is always best to display your actual raw data worksheet(s), and, the results that you are looking for. This way we can usually find a solution on the first go.

And, we should not have to create sample data.

I would have to see your actual raw data workbook/worksheet, with a separate worksheet with the list of like 20 companies.

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top