Copy all rows between two values where the first value is variable

abbasab

New Member
Joined
Sep 21, 2018
Messages
1
0
down vote
favorite
I want to loop and extract all the rows between two different words (For Example, 56050067 and 56050068) that repeat in a column. This code get me just the first range

Thank you for intance

Code:

Sub copy()

Dim rownum As Long Dim colnum As Long Dim startrow As Long Dim endrow As Long Dim lastrow As Long Dim s As Range Dim e As Range rownum = 1 colnum = 1

lastrow = Worksheets("Sheet1").Range("A65536").End(xlUp).Row

With Sheets("Sheet1") 'or this can be any other sheet where you search

Set r = .Range("B:B").Find("56050067")
If Not r Is Nothing Then
Set e = .Range("B:B").Find("56050068", r).Offset(-1)
If Not e Is Nothing Then

.Range(r, e).EntireRow.copy Sheets("Sheet2").Range("A1") 'or to whatever sheet
End If
End If
End With

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Cross posted https://stackoverflow.com/questions...-two-values-where-the-first-value-is-variable

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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