Search value, offset 3 columns, select and paste into new worksheet

jgravesNew

New Member
Joined
Jun 11, 2015
Messages
6
Hi all,
I've been trying for a while to get this to work! I am looking to find "Yes" in my column D range (D24:D75). If found, move to the corresponding cell in column A, copy that value and move it into row 3 in my Target sheet. Then find next "Yes", go to column A, copy, and paste to row 4 in my Target sheet...and so on.

Any help is greatly appreciated!

Sub CopyYes()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

Set Source = ActiveWorkbook.Worksheets("Rebalance")
Set Target = ActiveWorkbook.Worksheets("SellList")

j = 3 ' Start copying to row 3 in target sheet
For Each c In Source.Range("D24:D75")
If c = "Yes" Then
Cells.Activate
With ActiveCell
ActiveCell.Offset(0, -3).Copy Destination:=Target.Rows(j)
j = j + 1
End With
End If
Next c
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi there, thank you for the help!
looks like it still hits an application-defined or object-defined error here:
ActiveCell.Offset(0, -3).Copy Destination:=Target.Cells(j, 1)
 
Upvote 0
How about
Code:
Sub CopyYes()
   Dim c As Range
   Dim j As Integer
   Dim SourceWs As Worksheet, TargetWs As Worksheet
   
   Set SourceWs = ActiveWorkbook.Worksheets("Rebalance")
   Set TargetWs = ActiveWorkbook.Worksheets("SellList")
   
   j = 3 ' Start copying to row 3 in target sheet
   For Each c In Source.Range("D24:D75")
      If c = "Yes" Then
         c.Offset(0, -3).Copy Destination:=Target.Cells(j, 1)
         j = j + 1
      End If
   Next c
End Sub
 
Upvote 0
Unfortunately not, still tripping up here. Receiving an Object Required error on this line. When I step through, once it comes to a "Yes" in my column D range (D24:D75) then it errors.
c.Offset(0, -3).Copy Destination:=Target.Cells(j, 1)
 
Upvote 0
Oops missed a correction. It should be
Code:
         c.Offset(0, -3).Copy Destination:=TargetWs.Cells(j, 1)
I change the variable Target to TargetWs as it's best to avoid using VBA keywords as variables
 
Upvote 0
Perfect, thank you very much! Funny, I caught it in For Each c In Source.Range("D24:D75") and updated to SourceWs there.

Missed this one. Appreciate the help!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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