+ Reply to Thread
Results 1 to 11 of 11

Macro to Copy data from one sheet to another sheet if column headings match

Hybrid View

harman83 Macro to Copy data from one... 11-04-2015, 12:55 AM
:) Sixthsense :) Re: Macro to Copy data from... 11-04-2015, 01:09 AM
harman83 Re: Macro to Copy data from... 11-04-2015, 01:27 AM
harman83 Re: Macro to Copy data from... 11-04-2015, 01:30 AM
:) Sixthsense :) Re: Macro to Copy data from... 11-04-2015, 01:30 AM
harman83 Re: Macro to Copy data from... 11-04-2015, 01:35 AM
:) Sixthsense :) Re: Macro to Copy data from... 11-04-2015, 01:57 AM
harman83 Re: Macro to Copy data from... 11-04-2015, 02:08 AM
:) Sixthsense :) Re: Macro to Copy data from... 11-04-2015, 02:10 AM
harman83 Re: Macro to Copy data from... 11-04-2015, 02:26 AM
:) Sixthsense :) Re: Macro to Copy data from... 11-04-2015, 02:40 AM
  1. #1
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Macro to Copy data from one sheet to another sheet if column headings match

    Friends, I need your help to modify the macro

    I need help to create Macro to Copy data from one sheet to another sheet if column headings match. In the below code when in the Headers column there is First Name column, it will copy the column from sheet 1 and paste the same under First Name column for sheet 2.

    Please I need your help to modify the code and help me copy "Last name" and "country name" column data in the similar way from sheet 1 to sheet 2


    Option Explicit
    Sub Test()
    Dim ched, phed, lr As Long
    Sheets("Sheet1").Select
    ched = Application.WorksheetFunction.Match("First Name", Sheets("Sheet1").RANGE("1:1"), False)
    phed = Application.WorksheetFunction.Match("First Name", Sheets("Sheet2").RANGE("1:1"), False)

    lr = Sheets("Sheet1").Cells(Rows.Count, ched).End(xlUp).Row

    RANGE(Cells(2, ched), Cells(lr, ched)).Copy Sheets("Sheet2").Cells(2, phed)
    Sheets("Sheet2").Select

    RANGE(Cells(2, phed), Cells(Rows.Count, phed).End(xlUp)).NumberFormat = "DD/MM/YY"

    End Sub

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Sub Test()
    Dim ched, phed, lr As Long
    Sheets("Sheet1").Select
    
    'First Name Copy Paste Process
    '---------------------------------------------------------------------------------------------
    ched = Application.WorksheetFunction.Match("First Name", Sheets("Sheet1").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("First Name", Sheets("Sheet2").Range("1:1"), False)
    
    lr = Sheets("Sheet1").Cells(Rows.Count, ched).End(xlUp).Row
    
    Range(Cells(2, ched), Cells(lr, ched)).Copy Sheets("Sheet2").Cells(2, phed)
    '---------------------------------------------------------------------------------------------
    
    Sheets("Sheet2").Select
    Range(Cells(2, phed), Cells(Rows.Count, phed).End(xlUp)).NumberFormat = "DD/MM/YY"
    
    'Last name Copy Paste Process
    '---------------------------------------------------------------------------------------------
    ched = Application.WorksheetFunction.Match("Last name", Sheets("Sheet1").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("Last name", Sheets("Sheet2").Range("1:1"), False)
    
    lr = Sheets("Sheet1").Cells(Rows.Count, ched).End(xlUp).Row
    
    Range(Cells(2, ched), Cells(lr, ched)).Copy Sheets("Sheet2").Cells(2, phed)
    '---------------------------------------------------------------------------------------------
    
    'country name Copy Paste Process
    '---------------------------------------------------------------------------------------------
    ched = Application.WorksheetFunction.Match("country name", Sheets("Sheet1").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("country name", Sheets("Sheet2").Range("1:1"), False)
    
    lr = Sheets("Sheet1").Cells(Rows.Count, ched).End(xlUp).Row
    
    Range(Cells(2, ched), Cells(lr, ched)).Copy Sheets("Sheet2").Cells(2, phed)
    '----------------------------
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Thanks for the response Sixth sense...Really appreciated

    I am facing the following issues-
    Issue 1 - On running the code getting this error msg - Run Time error '9',Subscript out of range

    And it points to this code row -
    phed = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet2").Range("1:1"), False)

    Issue 2 - Last Name column data is same as First Name column data and Country Name column data is blank.

    Please note - I have changed headers names in your code , hope I have done it correctly
    ched = Application.WorksheetFunction.Match("Last name", Sheets("country name").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("Last name", Sheets("country name").Range("1:1"), False)

    to

    ched = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet1").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet2").Range("1:1"), False)

  4. #4
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Thanks for the response Sixth sense...Really appreciated

    I am facing the following issues-
    Issue 1 - On running the code getting this error msg - Run Time error '9',Subscript out of range

    And it points to this code row -
    phed = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet2").Range("1:1"), False)

    Issue 2 - Last Name column data is same as First Name column data and Country Name column data is blank.

    Please note - I have changed headers names in your code , hope I have done it correctly
    ched = Application.WorksheetFunction.Match("Last name", Sheets("country name").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("Last name", Sheets("country name").Range("1:1"), False)

    to

    ched = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet1").Range("1:1"), False)
    phed = Application.WorksheetFunction.Match("Country Name", Sheets("Sheet2").Range("1:1"), False)

  5. #5
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are demonstrated, mock them up manually if needed. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  6. #6
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Mock Data sheet attachedColumn Data.xlsx

    Sheet 1 column data needs to be copied to Sheet 2 with headers which matched with Sheet 1 headers. before running the code,sheet 2 has only headers

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    Sub Test()
    Dim wCopy As Worksheet, wPaste As Worksheet, iCol As Integer
    Dim vMatch As Variant, lRw As Long
    
    With ThisWorkbook
        Set wCopy = .Sheets("Sheet1")
        Set wPaste = .Sheets("Sheet2")
    End With
    
    Application.ScreenUpdating = False
    
    With wPaste
        iCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        .Range(.Cells(2, "A"), .Cells(Rows.Count, iCol)).ClearContents
    End With
    
    With wCopy
        For i = 1 To iCol
            vMatch = Application.Match(wPaste.Cells(1, i).Value, .Range("1:1"), 0)
            If IsNumeric(vMatch) Then
                lRw = .Cells(Rows.Count, vMatch).End(xlUp).Row
                .Range(.Cells(2, vMatch), .Cells(lRw, vMatch)).Copy wPaste.Cells(2, i)
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub

  8. #8
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Thanks for the code..

    On running the code getting this error msg - Run Time error '9',Subscript out of range
    This is the line where code is pointing to debug...Set wCopy = .Sheets("Sheet1")

    I followed the instruction givenColumn Data.xlsm

  9. #9
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Check this file
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    10-01-2015
    Location
    Delhi
    MS-Off Ver
    2013
    Posts
    105

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    You are a ROCKSTAR..this file is working....What did you do which I was missing while working on the code you provided? That will help me to learn from the mistakes.

  11. #11
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Macro to Copy data from one sheet to another sheet if column headings match

    Quote Originally Posted by harman83 View Post
    What did you do which I was missing while working on the code you provided? That will help me to learn from the mistakes.
    I am not sure whats wrong at your end that's why I have uploaded the file with code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Macro - Copy and paste data match by column headings
    By weijianhk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2015, 11:05 PM
  2. Replies: 3
    Last Post: 11-06-2014, 01:23 AM
  3. Macro button to copy data from one sheet to another sheet's next available column
    By Alice21 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-23-2013, 08:02 AM
  4. Copy/Paste filtered data from one sheet to another with only matched column headings
    By archerrc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-08-2013, 05:00 PM
  5. Replies: 7
    Last Post: 12-03-2012, 03:10 AM
  6. macro - match data and copy to new sheet
    By chizzle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2012, 06:56 PM
  7. Match column from sheet 2 to sheet 1 and copy rows if match exists.
    By GravityInvert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2008, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1