I've been using a VBA macro to copy cells from one sheet to another, but I now to make a slight modification which I'm struggling with. My sheets look as follows:
Sheet 1
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code
[/TD]
[TD]Description
[/TD]
[TD]Unit
[/TD]
[TD]Price
[/TD]
[/TR]
[TR]
[TD]AB
[/TD]
[TD]Item 1
[/TD]
[TD]KG
[/TD]
[TD]10
[/TD]
[/TR]
[TR]
[TD]CD
[/TD]
[TD]Item 2
[/TD]
[TD]Litre
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]EF
[/TD]
[TD]Item 3
[/TD]
[TD]KG
[/TD]
[TD]30
[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Unit
[/TD]
[TD]Code
[/TD]
[TD]Description
[/TD]
[TD]Price
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]CD
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]EF
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]AB
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For each of the codes in Sheet 2 I need to look up the code in Sheet and populate the cells accordingly. The problem I have is that the columns in Sheet don't match the order of the columns in Sheet 1. The macro I've been using below populates the columns after Code in order. Please can someone help me modify this:
Sub ()
Dim myCell As Range, FndCell As Range
For Each myCell In Sheets("Sheet1").Range("A2:A" & Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row)
Set FndCell = Sheets("Sheet2").Range("A1:A" & Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row).Find(myCell.Value, , xlValues, , xlByRows, xlNext)
If Not FndCell Is Nothing Then
myCell.Offset(, 1).Resize(1, 3).Copy FndCell.Offset(, 1)
End If
Next
End Sub
Sheet 1
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Code
[/TD]
[TD]Description
[/TD]
[TD]Unit
[/TD]
[TD]Price
[/TD]
[/TR]
[TR]
[TD]AB
[/TD]
[TD]Item 1
[/TD]
[TD]KG
[/TD]
[TD]10
[/TD]
[/TR]
[TR]
[TD]CD
[/TD]
[TD]Item 2
[/TD]
[TD]Litre
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]EF
[/TD]
[TD]Item 3
[/TD]
[TD]KG
[/TD]
[TD]30
[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Unit
[/TD]
[TD]Code
[/TD]
[TD]Description
[/TD]
[TD]Price
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]CD
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]EF
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]AB
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For each of the codes in Sheet 2 I need to look up the code in Sheet and populate the cells accordingly. The problem I have is that the columns in Sheet don't match the order of the columns in Sheet 1. The macro I've been using below populates the columns after Code in order. Please can someone help me modify this:
Sub ()
Dim myCell As Range, FndCell As Range
For Each myCell In Sheets("Sheet1").Range("A2:A" & Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row)
Set FndCell = Sheets("Sheet2").Range("A1:A" & Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row).Find(myCell.Value, , xlValues, , xlByRows, xlNext)
If Not FndCell Is Nothing Then
myCell.Offset(, 1).Resize(1, 3).Copy FndCell.Offset(, 1)
End If
Next
End Sub