Copy data from one worksheet to another by matching column headers

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
I am using the code below, which works beautifully to insert the new column headings I need. Now, I am trying to come up with a way to look at the "N" columns inserted between column B and (former) column C as a result of the code (below)... and if this column heading also appears in row 13 of the "Staffing Plan" worksheet, then I need to copy the data in that column from the "Staffing Plan" worksheet and paste it into the matching column on the "Import" worksheet.

Does anyone have an efficient method in mind to make this happen?
Code:
Sub InsertN()
 Dim N As Long
 N = Sheets("Pricing").Range("I23")
 With Sheets("Import")
       .Columns("C").Resize(, N).Insert
       Sheets("Pricing").Range("E9:E" & 9 + N - 1).Copy
       .Range("C1").PasteSpecial Paste:=xlValues, Transpose:=True
       Application.CutCopyMode = False
 End With
 End Sub



On "Pricing" worksheet:
  • In column E (from row 9+)...I have a list that the user will input values - which will become column headings
  • In cell $I$23 I have a count of how many values are inserted into this range ("N")


On "Import" worksheet:
  • It inserts "N" columns between column B and column C
  • Then it copies the list from column E on "Pricing", transposes and pastes values into row 1 of "import"...in the columns that were just added between column B and (former) column C




EXAMPLE:


"Pricing" Worksheet
"New Heading 1" ..... E9
"New Heading 2" ..... E10

As a result.... $I$23 = 2


"Import" Worksheet

(before existing code above)

Column A
Column
B

Column
C

Column D
entry 1
entry 1
entry 1
entry 1
entry 2
entry 2
entry 2
entry 2

<tbody>
</tbody>



(after existing code above)

Column A
<new 1="" Heading="">Column B</new>
<new 2="" Heading="">NEW HEADING 1</new>
NEW HEADING 2
Column C
Column D
entry 1
entry 1
(blank)
(blank)
entry 1
entry 1
entry 2
entry 2
(blank)
(blank)
entry 2
entry 2

<tbody>
</tbody>



(RESULT AFTER CODE MODIFICATION)

#1: for this example, lets say that only "NEW HEADING 1" was found on the "Staffing Plan" worksheet in row 13....
Column A
<new 1="" Heading="">Column B</new>
<new 2="" Heading="">NEW HEADING 1</new>
NEW HEADING 2
Column C
Column D
entry 1
entry 1
ABC
(blank)
entry 1
entry 1
entry 2
entry 2
XYZ
(blank)
entry 2
entry 2

<tbody>
</tbody>



#2: for this example, lets say that BOTH "NEW HEADING 1" and "NEW HEADING 2" were found on the "Staffing Plan" worksheet in row 13....
Column A
<new 1="" Heading="">Column B</new>
<new 2="" Heading="">NEW HEADING 1</new>
NEW HEADING 2
Column C
Column D
entry 1
entry 1
ABC
DEF
entry 1
entry 1
entry 2
entry 2
XYZ
UVW
entry 2
entry 2

<tbody>
</tbody>
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi everyone - I was able to re-use some code from an old worksheet, but I can't seem to get it to paste values. Also, I think it's copying the entire column - I will need to import this into a system (these "blank" rows will cause error), so I was wondering if anyone can help me get it to only copy to the last row used.

Thanks again for any guidance and support.

Code 1: INSERT COLUMNS/COLUMN HEADERS
Code:
Sub InsertN()
 Dim N As Long
 N = Sheets("Pricing").Range("I23")
 With Sheets("Import")
       .Columns("C").Resize(, N).Insert
       Sheets("Pricing").Range("E9:E" & 9 + N - 1).Copy
       .Range("C1").PasteSpecial Paste:=xlValues, Transpose:=True
       Application.CutCopyMode = False
 End With
 End Sub


Code 2: COPY DATA INTO NEW COLUMNS BASED ON HEADER
Code:
Sub Copy_Data()
    Dim header As Range, headers As Range
    
    Set headers = Worksheets("Staffing Plan").Range("M13:AC13")
    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("Res Hrs Cost-PP").Cells(2, GetHeaderColumn(header.Value))
        End If
    Next
End Sub

Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("Res Hrs Cost-PP").Range("C1:K1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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