VBA copy multiple delimited ranges from one column to new worksheet with a string

setai25

New Member
Joined
May 30, 2012
Messages
11
Hello,

I have an enormous data set in a single column (180k line config file dump), and need to copy any ranges where the first cell contains a string. The ranges are delimited by a specific series of cells, and the first cell in a range starts with the same text string. The string I need to search by is a 7-digit id number.

I found a script that copies cells containing a string to a new worksheet: Search for substring and copy results to new worksheet?
This is half of what I am looking to accomplish.
I would like to copy the entire ranges of cells where the first cell in the range contains the specified string.

Sample Data
ssssssssss
ssssssssss
ACO: site.com: 1234567 - name1
DataPoint1
DataPoint2
DataPoint3
DataPoint4
ssssssssss
ssssssssss
ACO: site2.com: 7539516 - name2
DataPoint2
DataPoint4
DataPoint4
ssssssssss
ssssssssss

<tbody>
</tbody>


This is the code DMcClenagan posted:

Code:
Sub FindStringManyTimes()
    Dim ii As Integer
    Dim gFind, gPlug As Long
    Dim bRet As Boolean
    Dim oo As Object
    
    Sheets.Add
    ActiveSheet.Name = "ListOfFinds"
    For ii = 2 To Sheets.Count
        With Sheets(ii)
            bRet = bAnyCells(Sheets(ii).Name)
            If bRet Then
                For Each oo In .Cells.SpecialCells(xlCellTypeConstants)
                    gFind = InStr(1, oo.Value, "ACO: ")
                    If gFind > 0 Then
                        gPlug = gPlug + 1
                        Cells(gPlug, 1).Value = oo.Value
                        Cells(gPlug, 2).Value = Sheets(ii).Name
                        Cells(gPlug, 3).Value = oo.Address(False, False)
                    End If
                Next oo
            End If
        End With
    Next ii
End Sub
Function bAnyCells(sSheet As String) As Boolean
    Dim oo As Object
    On Error GoTo Bye
    With Sheets(sSheet)
        For Each oo In .Cells.SpecialCells(xlCellTypeConstants)
        Next oo
    End With
    bAnyCells = True
Bye:
    On Error GoTo 0
End Function


Many thanks, in advance.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I am guessing that for the example below, you'll want to copy all the lines highlighted if you are searching for 1234567?

The code you've posted is based on looping, and will take a long time with 180,000 rows of data. Better to filter and copy in bulk. Here's one way you could do this, using a helper column:

B2: =IF(LEFT(A2,4)="ACO:",TRIM(MID(SUBSTITUTE(TRIM(A2)," ",REPT(" ",50)),100,50)),IF(ISBLANK(A2),0,B1))

This assumes your data is well behaved, and ID is located between 2nd and 3rd space(?)

Now simply filter and copy/paste the filtered results.

If you need to repeat for multiple IDs, it will be relatively simple to write a macro to loop through.

Excel Workbook
AB
1Sample Data
2ssssssssss0
3ssssssssss0
4ACO: site.com: 1234567 - name11234567
5DataPoint11234567
6DataPoint21234567
7DataPoint31234567
8DataPoint41234567
9ssssssssss1234567
10ssssssssss1234567
110
12ACO: site2.com: 7539516 - name27539516
13DataPoint27539516
14DataPoint37539516
15DataPoint47539516
160
17ACO: site.com: 1234567 - name31234567
18DataPoint11234567
19DataPoint21234567
Sheet1
 
Upvote 0
That is pretty good...the data isn't as well behaved as I'd like. However, cleaning isn't a tough job with this solution (it shows where human data entry missed).

As "they" say, perfect is the enemy of complete. I think this will get me there.

Thanks, very much.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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