Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:11:45+00:00 2026-06-09T20:11:45+00:00

I have small set of data in excel with 4 columns File A: SNO

  • 0

I have small set of data in excel with 4 columns

File A: 

  SNO   TYPE  CountryA   CountryB   CountryD
    1    T1    A1          B2         D1          
    2    T2    A2          B2         D2

and i have this data in another excel file

File B:

   SNO   TYPE  CountryB  CountryA CountryC
    11    T10   B10         A10     C10
    22    T20   B20         A20     C20
    33    T30   B30         A30     C30

Now if i want to paste the data in file B over the data in file A, i want the column names to align automatically using some vba code.

So the End result should look like,

       SNO  TYPE CountryA    CountryB  CountryC  CountryD           
        1    T1   A1           B1         --         D1
        2    T2   A2           B2         --         D2 
        11   T10  A10          B10        C10        --
        22   T20  A20          B20        C20        --
        33   T30  A30          B30        C30        -- 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:11:46+00:00Added an answer on June 9, 2026 at 8:11 pm

    This should work for you:

    Sub MatchUpColumnDataBasedOnHeaders()
    
    Dim wbk As Workbook
    Set wbk = ThisWorkbook
    Set ws = wbk.Sheets(1)
    Set ws2 = wbk.Sheets(2)
    Dim cell As Range
    Dim refcell As Range
    
    Application.ScreenUpdating = False
    ws.Select
    
        For Each cell In ws.Range("A1:Z1")
    
            cell.Activate
            ActiveCell.EntireColumn.Copy
    
            For Each refcell In ws2.Range("A1:Z1")
                If refcell.Value = cell.Value Then refcell.PasteSpecial (xlPasteValues)
            Next refcell
    
        Next cell
    Application.ScreenUpdating = True
    
    End Sub
    

    It’s funny, I have this feeling there is a really easy non-VBA way to do this – but I couldn’t find the button for it on google. This will work for columns A to Z on sheets 1 and 2. This assumes your headers are in row 1.

    EDIT – IN ADDITION:

    I noticed that you wanted to do this with files and you didn’t say anything about sheets. This is how you would do it with different workbooks:

    Sub MatchUpColumnDataBasedOnHeadersInFiles()
    
    Dim wbk As Workbook
    
    Set wbk = ThisWorkbook
    
    Workbooks.Open Filename:="C:\PasteIntoWorkbook.xlsx"
    Set wbk2 = Workbooks("PasteIntoWorkbook.xlsx")
    
    Set ws = wbk.Sheets(1)
    Set ws2 = wbk2.Sheets(1)
    
    Dim cell As Range
    Dim refcell As Range
    
    wbk.Activate
    
    Application.ScreenUpdating = False
    
    ws.Select
    
        For Each cell In ws.Range("A1:N1")
    
            wbk.Activate
            ws.Select
    
            cell.Activate
            ActiveCell.EntireColumn.Copy
    
            wbk2.Activate
            ws2.Select
    
            For Each refcell In ws2.Range("A1:N1")
                If refcell.Value = cell.Value Then refcell.PasteSpecial (xlPasteValues)
            Next refcell
    
        Next cell
    
    ws2.Select
    Range("A1").Select
    wbk.Activate
    ws.Select
    Range("A1").Select
    
    Application.ScreenUpdating = True
    
    End Sub
    

    So if were heart-set on working with different .xls files, then that is how yo would do that. You obviously would just need to adjust the file path to whatever your paste-into file would be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a small data set and would like to search through it in
In my c# application i have this small form which is used to set
I have a set of data in Excel and in one column is a
I have a small set of structured data items that I would like embedded
I have set of 200 data rows(implies a small set of data). I want
I have a customer with a very small set of data and records that
I have a small data set of a moving car: Data_=[time x,y,z]; %# ONLY
I have to implement an application in which I have set small edittext for
i have a small problem where my mandelbrot set is represented as a heightmap
I have a small shell application that embeds Tcl to execute some set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.