Match column headers and copy over data

suptechguy

New Member
Joined
Jan 15, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am starting a new project to simplify an uploading process. I would like to create a workbook that is set up like this:

Two sheets, one is called “Upload Data”, and the other is called “Prepared File. The “Prepared File” sheet has the column headers in the correct order for the upload to work properly. When users copy and paste data into the “Upload Data” sheet, then the data for each column will copy into sheet “Prepared File’ and will be arranged in the correct order.



I think the logic would look something like this:

In sheet (“Prepared File”) in range (“9:9”)

Match each column header (A9, B9, C9, …) with the column headers in sheet (“Upload data”) in range (“A:A”).

If exact match is found, copy the data in that column from Sheet (“Upload Data”) into the matching column on sheet (“Prepared File”)

If a match is not found, then put a (“x”) above that column (A8, B8, C8, …) in sheet (“Prepared File”)



Please help. Thank!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Welcome to the MrExcel board!

Are you looking for a macro or a formula approach?
 
Upvote 0
So basically, each column header in row (10:10) of sheet ("Prepared File") needs to find the matching column header in row (1:1) of sheet ("Upload Data")
If a match is found, copy the data in that column of sheet ("Upload Data") into the matching column of sheet ("Prepared File")
'If there is no match, place an 'x' above the column header in sheet ("Prepared File") to show that there was no match

I think VBA will be the best choice now
 
Upvote 0
I think VBA will be the best choice now
Give this a try in a copy of your workbook.

VBA Code:
Sub Copy_Columns()
  Dim wsUD As Worksheet, wsPF As Worksheet
  Dim rCell As Range, rFound As Range
  
  Set wsUD = Sheets("Upload Data")
  Set wsPF = Sheets("Prepared File")
  With wsPF
    For Each rCell In .Range("A10", .Range("A10").End(xlToRight))
      Set rFound = wsUD.Rows(1).Find(What:=rCell.Value, LookAt:=xlWhole, MatchCase:=False)
      If rFound Is Nothing Then
        rCell.Offset(-1).Value = "x"
      Else
        wsUD.Range(wsUD.Cells(2, rFound.Column), wsUD.Cells(Rows.Count, rFound.Column).End(xlUp).Offset(1)).Copy Destination:=rCell.Offset(1)
      End If
    Next rCell
  End With
End Sub

My sample data

Book1
ABC
1GuyOtherSuper
2Guy 1Other 1Super 1
3Guy 2Other 2Super 2
4Guy 3Other 3Super 3
5Guy 4Other 4Super 4
6Guy 5Super 5
7Guy 6Super 6
8Guy 7
9
Upload Data



Results of code when this sheet originally just had the green cells.

Book1
ABC
9x
10SuperTechGuy
11Super 1Guy 1
12Super 2Guy 2
13Super 3Guy 3
14Super 4Guy 4
15Super 5Guy 5
16Super 6Guy 6
17Guy 7
18
Prepared File
 
Upvote 0
This is brilliant thank you! What if I want an "x" to also appear above the column headers of sheet ("Upload Data"). That way, users can see which columns they need to fix on the Upload Tab. When they fix the columns and press the button again, the columns with the x's should reset. Can this be adjusted?
 
Upvote 0
What if I want an "x" to also appear above the column headers of sheet ("Upload Data").
In my example do you mean mark the 'Other' column in 'Upload Data'?

If so try adding these two blue lines where shown.

Rich (BB code):
  Set wsPF = Sheets("Prepared File")
  wsUD.Range("A1", wsUD.Cells(1, Columns.Count).End(xlToLeft)).Interior.Color = vbYellow
  With wsPF
.
.
      Else
        rFound.Interior.Color = xlNone
        wsUD.Range(wsUD.Cells(2, rFound.Column), wsUD.Cells(Rows.Count, rFound.Column).End(xlUp).Offset(1)).Copy Destination:=rCell.Offset(1)

If that is not what you meant, please clarify further.
 
Upvote 0
Sorry for the delay in response..
Thank you very much! That's exactly what I was looking for

The last issue I am having with it is this:
The highlighting (now red) on sheet ("Upload Data") is happening on the exact row of the headers but I would like it to happen to the cells above the headers. When I try to adjust the code, the highlighting will highlight both the header row and the row above it but only the header row will use the matching logic to choose which cells to highlight; meaning the row above the headers will be completely highlighted red no matter the matching.

Here's what I got for code:
VBA Code:
Private Sub CommandButton1_Click()
 Dim wsUD As Worksheet, wsPF As Worksheet
  Dim rCell As Range, rFound As Range
  
  Set wsUD = Sheets("Upload Data")
  Set wsPF = Sheets("Prepared File")
  
  wsPF.Range("9:9").ClearContents
  wsPF.Range("11:50000").ClearContents
  wsUD.Range("A3", wsUD.Cells(3, Columns.Count).End(xlToRight)).Interior.Color = vbRed
  With wsPF
    For Each rCell In .Range("A10", .Range("A10").End(xlToRight))
      Set rFound = wsUD.Rows(3).Find(What:=rCell.Value, LookAt:=xlWhole, MatchCase:=False)
      If rFound Is Nothing Then
        rCell.Offset(-1).Value = "x"
        '****
       
        '****
      Else
      rFound.Interior.Color = xlNone
        wsUD.Range(wsUD.Cells(4, rFound.Column), wsUD.Cells(Rows.Count, rFound.Column).End(xlUp).Offset(1)).Copy Destination:=rCell.Offset(1)
      End If
    Next rCell
  End With
  
  
End Sub
 
Upvote 0
Not certain I have understood what you want on wsUD, by try making these 2 changes

Rich (BB code):
wsUD.Range("A3", wsUD.Cells(3, Columns.Count).End(xlToRight)).Interior.Color = vbRed
wsUD.Range("A3", wsUD.Cells(3, Columns.Count).End(xlToLeft)).Offset(-1).Interior.Color = vbRed


rFound.Interior.Color = xlNone
rFound.Offset(-1).Interior.Color = xlNone
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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