Copy rows to sheet2 based on a value search in a range and move only selected columns

bulletshiva

New Member
Joined
Jun 6, 2018
Messages
11
Hello,

I have a task to move the rows to sheet 2 based on a search value in a range. The below code will search a particular column and copies all the rows to sheet2.
Now i want to check a range of columns for a value and copy only required columns like Item_id and description to sheet2.

request you to modify the code.

Item idDescription
Date
Range
A002M22002pump2/8/2018KLZXQS
A002M22003handle1/8/2018KM
Z
L
QS
A002M22004washer5/8/2018KN
ZXL
E

<colgroup><col><col span="2"><col span="6"></colgroup><tbody>
</tbody>

Sub Button1_Click()
a = Worksheets("sheet1").Cells(Row.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("sheet1").Cells(i, 3).Value = "L" Then
Worksheets("sheet1").Row(i).Copy
Worksheets("sheet2").Activate
b = Worksheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("sheet1").Cells(1, 1).Select
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
Code:
Sub Button1_Click()
Dim a As Long, b As Long, r As Long, c As Long
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).row
b = Worksheets("sheet1").Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
For r = 2 To a
   For c = 4 To b
      If Worksheets("sheet1").Cells(r, c).Value = "L" Then
         Worksheets("sheet1").Range("A" & r).Resize(, 2).Copy Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
         Exit For
      End If
   Next c
Next r
ThisWorkbook.Worksheets("sheet1").Cells(1, 1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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