Macro to Copy Entitre column of Matching Column Header & Paste to Matching Column Header

zach9208

Board Regular
Joined
Dec 15, 2015
Messages
117
Hi There,

I need help building a macro that copies entire columns from one workbook over to another based on matching column names in row 1. These copied columns would then need pasted in another workbook based on the matching column names.

Another way of saying this is to use macros to do a H-Lookup and paste entire columns to another workbook.I have done this before from the source to a blank but now I need to match the source column name and pull those columns to the matching column name of the destination workbook.

For example: workbook 1 has a bunch of columns. I have three columns mixed in there called "TEST1", "TEST5", TEST17". I need to copy these three named columns over to Workbook 2 in the tab called existing and paste these in the columns sharing these three names. The column order can vary between the files hence why I am trying to copy/paste based on the matching column names that I specify in the macro.

I also need this to be a loop and copy data from multiple tabs to paste into the consolidated workbook. These tabs are called Source1, Source2, source3 and Source4.


Here is the code I had for a similar project that was just copy certain columns into a brand new workbook..

Code:
Sub CreateUserInitiatedLoadCSV()
Dim wbNew As Workbook, wbSrc As Workbook, Error As Range
Dim SaveToDirectory$, CurrentWorkbook$, KeepRunning As VbMsgBoxResult
Dim CurrentFormat&, nmary, sh1 As Worksheet, sh2 As Worksheet, i&, rng As Range

    Call FormatColumnstoText
    Set wbSrc = ThisWorkbook


    nmary = Array("TEST1", "TEST5", "TEST7")
    Set wbNew = Workbooks.Add


    Set sh2 = wbNew.Sheets(1)
    For i = LBound(nmary) To UBound(nmary)
        Set rng = sh1.Rows(4).Find(nmary(i), , xlValues).EntireColumn
        rng.Copy sh2.Cells(1, i + 1)
    Next
     
   
   ' Store current details for the workbook
    CurrentWorkbook = ThisWorkbook.FullName
    CurrentFormat = ThisWorkbook.FileFormat
    
    SaveToDirectory = "C:\Users\c755748\Desktop\" 
    wbNew.SaveAs Filename:=SaveToDirectory & "Counterparty_Ratings" & ".xlsx", FileFormat:=xlsx
    
    wbNew.Close savechanges:=False


    Application.DisplayAlerts = False
    ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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