WISDOMBYDATA
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me

How to transpose data from one workbook to another based on matching header names

12/27/2018

0 Comments

 
This week I want to demonstrate an efficient method for transferring desired data fields from one workbook to another without needing to manually copy/paste the data. This method will particularly come in handy in large spreadsheets where many columns need to be transferred.

The data set below contains product categories for a retail store and the daily sales for each category. The goal is to transfer the fields belonging to the Clothing categories (i.e. Womenswear, Sportswear,  Swimsuits, and Footwear) from the “All Products” tab to the “Apparel” tab. 

Picture
Here are the steps:

STEP 1] copy paste the name of the desired fields (i.e. Date, Womenswear, Sportswear, Swimsuits, and Footwear) into the second tab. Spelling musty be exactly the same.
Picture
​STEP 2] copy and paste the macro below into the module tab of the VBA editor.

Sub FieldLookup()
 
Dim Sht1 As Worksheet, Sht2 As Worksheet
Dim Sht1Head As Range, Sht2Head As Range
Dim Header1 As Range, Header2 As Range
Dim Counter As Integer
Counter = 1
Set Sht1 = Sheets("Apparel")
Set Sht2 = Sheets("All Products")
Dim lastCol As Long
 
Application.ScreenUpdating = False
 
'Headers from the first sheet-starting from cell A1
lastCol = Sht1.Cells(1, Columns.Count).End(xlToLeft).Column
Set Sht1Head = Sht1.Range("A1", Sht1.Cells(1, lastCol))
 
' Headers from the second sheet-starting from cell A1
lastCol = Sht2.Cells(1, Columns.Count).End(xlToLeft).Column
Set Sht2Head = Sht2.Range("A1", Sht2.Cells(1, lastCol))
 
'actually loop through and find values
Do While Counter < 365
For Each Header2 In Sht2Head
    For Each Header1 In Sht1Head
        If Header2.Value = Header1.Value Then
              Header2.Offset(Counter, 0).Copy
              Header1.Offset(Counter, 0).PasteSpecial xlPasteAll
            Application.CutCopyMode = False
        End If
    Next Header1
Next Header2
Counter = Counter + 1
Loop
 
End Sub
Picture
​
​
Few very important note about this macro:

First Note - Enter the name of the destination workbook on line 8

                                                    Set Sht1 = Sheets("Apparel")
 
Second Note - enter the name of the source workbook on line 9

                                                   Set Sht2 = Sheets("All Products")
 

Third Note - Enter the number of rows that you would like to be transfer on line 23

                                                   Do While Counter < 365
 
Here is the outcome in the second tab after the macro has been run:
Picture
0 Comments



Leave a Reply.

    Categories

    All
    BI
    EXCEL
    MISC
    SQL

    Archives

    June 2020
    May 2020
    April 2020
    March 2020
    February 2020
    December 2019
    November 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    May 2015
    April 2015
    March 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    April 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013

Picture
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me