Copying specific data to another sheet based on cell value

JJabra

New Member
Joined
Aug 19, 2019
Messages
12
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,247
Office Version
365
Platform
Windows
Which column do you want to copy the data to?
 

JJabra

New Member
Joined
Aug 19, 2019
Messages
12
Apologies,

I want the data to copy to columns A and B on the second sheet
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,247
Office Version
365
Platform
Windows
In that case use
Code:
Source.Range(Cells(c.Row, 1), Cells(c.Row, 2)).Copy Target.[COLOR=#ff0000]Cells(j,1)[/COLOR]
 

JJabra

New Member
Joined
Aug 19, 2019
Messages
12
Thanks! that works, one last thing, is it possible to match destination formatting? not a massive issue, however would be useful if it could
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,247
Office Version
365
Platform
Windows
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
 

JJabra

New Member
Joined
Aug 19, 2019
Messages
12
Amazing! thank you very much for your help!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,247
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

JJabra

New Member
Joined
Aug 19, 2019
Messages
12
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,247
Office Version
365
Platform
Windows
How about
Code:
Source.Range("A" & c.Row & ",F" & c.Row).Copy
Target.Cells(j, 1).PasteSpecial xlPasteValues
 

Forum statistics

Threads
1,077,849
Messages
5,336,736
Members
399,100
Latest member
darcob

Some videos you may like

This Week's Hot Topics

Top