Help with VBA - If Column is X , Select Row, Copy Cells and Paste on new sheet

gr1226

New Member
Joined
Apr 17, 2018
Messages
1
Help please! After browsing this thread and and others I have achieved to use this code to copy and past data dependent on a specific value in a column. I have 2 issues..

1. After the row is found I would like to copy certain cells of that row such as A C and F
2. I would like to then paste to sheet 2 in cells X, Y, Z

Code:
Sub Test ()

Dim xRg As Range
Dim xCell As Range
Dim i As Long
Dim J As Long
Dim K As Long
i = Worksheets("Data").UsedRange.Rows.count
J = Worksheets("Sheet2").UsedRange.Rows.count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("Data").Range("Y2:Y" & i)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To xRg.count
If CStr(xRg(K).Value) = "1" Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1)
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub
Thanks in advance
 
Last edited by a moderator:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi & welcome to MrExcel
How about
Code:
Sub Test()

   Dim xRg As Range
   Dim xCell As Range
   Dim i As Long
   Dim J As Long
   Dim K As Long
   i = Worksheets("Pcode").UsedRange.Rows.Count
   J = Worksheets("data").UsedRange.Rows.Count
   If J = 1 Then
      If Application.WorksheetFunction.CountA(Worksheets("data").UsedRange) = 0 Then J = 0
   End If
   Set xRg = Worksheets("pcode").Range("Y2:Y" & i)
   On Error Resume Next
   Application.ScreenUpdating = False
   For K = 1 To xRg.Count
      If CStr(xRg(K).Value) = "1" Then
      Intersect(xRg(K).EntireRow, Worksheets("pcode").Range("A:A,C:C,F:F")).Copy Destination:=Worksheets("data").Range("X" & J + 1)
      J = J + 1
      End If
   Next
   Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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