Copying specific data to another sheet based on cell value

JJabra

New Member
Joined
Aug 19, 2019
Messages
24
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
31,992
Office Version
365
Platform
Windows
Which column do you want to copy the data to?
 

JJabra

New Member
Joined
Aug 19, 2019
Messages
24
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
31,992
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
24
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
31,992
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
24
Amazing! thank you very much for your help!
 

Fluff

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

JJabra

New Member
Joined
Aug 19, 2019
Messages
24
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
31,992
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,081,561
Messages
5,359,612
Members
400,540
Latest member
JimUSMC

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top