fari1
Active Member
- Joined
- May 29, 2011
- Messages
- 362
hi,my below code finds a text fruit in the sheet and then in the next row,or next right or left rows looks for the word apple and then copies the next 500 rows of the row apple including apple row and paste it into the sheet2. this code is not working properly somehow, anyhelp on this would be greatly appreciated.
Code:
Sub Findfruit()
Application.ScreenUpdating = False
Dim target_fruit As String, fruitrow As Long, qtyrows As Long, targetfruit_row As Long
target_fruit = InputBox("What fruit are you looking for?", "Enter Fruit")
' **IF YOU ARE SET ON 500 ROWS TO COPY THEN YOU CAN COMMENT OUT THE NEXT 3 LINES AND THE 5th.
On Error Resume Next
qtyrows = InputBox("How many rows do you want to copy?", "Enter number of Rows")
If qtyrows < 1 Then
qtyrows = 500
End If
Cells.Find(What:=target_fruit, After:=Cells(ActiveCell.Row, "a"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
targetfruit_row = ActiveCell.Row
Cells.Find(What:="fruit", After:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
testagain:
If ActiveCell.Row <= targetfruit_row Then
fruitrow = ActiveCell.Row
Cells.Find(What:="fruit", After:=Cells(ActiveCell.Row, ActiveCell.Column), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
If ActiveCell.Row < fruitrow Then
GoTo continue
Else
GoTo testagain
End If
End If
continue:
Rows(fruitrow & ":" & fruitrow + qtyrows).Copy
Dim lastsheet2 As Long
lastsheet2 = Sheets("Sheet2").[a65536].End(xlUp).Rows
Sheets("sheet2").Activate
Sheets("Sheet2").Cells(lastsheet2 + 1, 1).Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Sub
Last edited: