Copying specific data to another sheet based on cell value

JJabra

New Member
Joined
Aug 19, 2019
Messages
37
Hi guys,

What I am looking to do is copy data from one sheet to another, however I only want it to copy columns A and B based on the cell value of Column E in each row

So far I have the following code which is able to copy the data, but currently pastes it on every column on the worksheet and I can't see why. Apologies if it is something simple, I am relatively new to VBA.

Code:
Sub HoldCodeSeparation()


Dim Source As Worksheet
Dim Target As Worksheet




Set Source = ActiveWorkbook.Worksheets("Hold Codes")
Set Target = ActiveWorkbook.Worksheets("Extract")


j = 2 ' Start copying to row 1 in target sheet
For Each c In Source.Range("E1:E1000") ' Do 1000 rows
If CStr(c) = "22" Then
Source.Range(Cells(c.Row, 1), Cells(c.Row, 2)).Copy Target.Rows(j)
j = j + 1
End If
Next c


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Which column do you want to copy the data to?
 
Upvote 0
In that case use
Code:
Source.Range(Cells(c.Row, 1), Cells(c.Row, 2)).Copy Target.[COLOR=#ff0000]Cells(j,1)[/COLOR]
 
Upvote 0
Thanks! that works, one last thing, is it possible to match destination formatting? not a massive issue, however would be useful if it could
 
Upvote 0
If you only want values use
Code:
Target.Cells(j, 1).Resize(, 2).Value = source.Range(Cells(c.Row, 1), Cells(c.Row, 2)).Value
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
I have been trying to amend the code to now copy data from Column A and F instead of A and B, the data would still be pasted to columns A and B on the target sheet, any suggestions?
 
Upvote 0
How about
Code:
Source.Range("A" & c.Row & ",F" & c.Row).Copy
Target.Cells(j, 1).PasteSpecial xlPasteValues
 
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