Conditionally copy cells into non-consecutive columns

ChrisG29

New Member
Joined
Jun 28, 2017
Messages
4
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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
ChrisG29,

Welcome to the MrExcel forum.

If I understand you correctly, then, here is a macro solution for you to consider.

Sample worksheets:


Excel 2007
ABCD
1CodeDescriptionUnitPrice
2ABItem 1KG10
3CDItem 2Litre20
4EFItem 3KG30
5
Sheet1



Excel 2007
ABCD
1UnitCodeDescriptionPrice
2CD
3EF
4AB
5
Sheet2


And, after the macro:


Excel 2007
ABCD
1UnitCodeDescriptionPrice
2LitreCDItem 220
3KGEFItem 330
4KGABItem 110
5
Sheet2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ChrisG29()
' hiker95, 06/29/2017, ME1011945
Dim w1 As Worksheet, w2 As Worksheet
Dim b As Range, a As Range
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
With w2
  For Each b In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
    Set a = w1.Columns(1).Find(b.Value, LookAt:=xlWhole)
    If Not a Is Nothing Then
      b.Offset(, -1).Value = w1.Cells(a.Row, 3).Value
      b.Offset(, 1).Value = w1.Cells(a.Row, 2).Value
      b.Offset(, 2).Value = w1.Cells(a.Row, 4).Value
    End If
  Next b
  .Columns.AutoFit
  .Activate
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ChrisG29 macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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