need to copy an unknown number of rows after finding a specific word until the next word comes along. This is the database:
<CODE>Counterparty | 721 | 721 Healthcare | CCY | Invoice amount
12/03/14 | 12/10/14 | 081673 | USD | 1000
12/22/14 | 12/22/14 | 081954 | USD | 999
Counterparty | 722 | 722 Healthcare | CCY | Invoice Amount
12/22/14 | 12/22/14 | 081954 | USD | 999
12/22/14 | 12/22/14 | 081954 | USD | 999</CODE>
</PRE>This goes on and the amount of rows vary every month. I am only required to find one of the company(represented by 721). And what i need is to copy from the heading : Counterparty, 721, 721 Healthcare, CCY, Invoice Amount. Followed by the data beneath it until it hits a different company. Basically copy from only 721 and all the information until it hit 722 and paste it onto a new worksheet.
What i got right now is this
It gives me error 9. Subscription out of range.
Thanks in advance guys
<CODE>Counterparty | 721 | 721 Healthcare | CCY | Invoice amount
12/03/14 | 12/10/14 | 081673 | USD | 1000
12/22/14 | 12/22/14 | 081954 | USD | 999
Counterparty | 722 | 722 Healthcare | CCY | Invoice Amount
12/22/14 | 12/22/14 | 081954 | USD | 999
12/22/14 | 12/22/14 | 081954 | USD | 999</CODE>
</PRE>This goes on and the amount of rows vary every month. I am only required to find one of the company(represented by 721). And what i need is to copy from the heading : Counterparty, 721, 721 Healthcare, CCY, Invoice Amount. Followed by the data beneath it until it hits a different company. Basically copy from only 721 and all the information until it hit 722 and paste it onto a new worksheet.
What i got right now is this
Code:
Sub bgtoutflow()
Dim SearchItem As String
Dim SearchResult As Range
Dim LastRowWS As Long
Dim SWS As Worksheet
Dim CopyRow As Long
Dim PasteSeq As Integer
Dim CopyStart As Long
Set SWS = Sheets("Sheet2")
SearchItem = "*721 :*"
PasteSeq = 0
CopyStart = 1
LastRowWS = Range("B241").End(xlDown).Row
For i = 1 To LastRowWS
With SWS.Range("B:B")
'dynamic range to find next item
Set SearchResult = .Find(What:=SearchItem, _
After:=Range("B" & i), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not SearchResult Is Nothing Then
PasteSeq = PasteSeq + 1 'For new sheetname
On Error GoTo LastRow 'For the last part
CopyRow = SearchResult.Offset(-1, 0).Row
Rows(CopyStart & ":" & CopyRow).Copy
Sheets.Add.Name = "PasteSheet" & PasteSeq
Sheets("PasteSheet" & PasteSeq).Range("A1").PasteSpecial xlPasteAll
Sheets("Search").Select
CopyStart = SearchResult.Row
i = CopyRow 'SearchResult.Row
End If
End With
Next
LastRow:
Rows(CopyRow + 1 & ":" & LastRowWS).Copy
Sheets.Add.Name = "PasteSheet" & PasteSeq
Sheets("PasteSheet" & PasteSeq).Range("A1").PasteSpecial xlPasteAll
Sheets("Search").Select
End Sub
It gives me error 9. Subscription out of range.
Thanks in advance guys