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
32,315
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
32,315
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
32,315
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
32,315
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
32,315
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,082,253
Messages
5,364,052
Members
400,776
Latest member
JimmyLee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top