+ Reply to Thread
Results 1 to 12 of 12

Copy Data from one sheet to another using header as key

  1. #1
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Question Copy Data from one sheet to another using header as key

    Holla,


    I have been trying to append multiple sheet data into a single one by sorting them with Header as a Key.

    Problem : I have a worksheet with Sheets : Sheet1, Sheet2, Sheet3, Sheet4,...... Sheet(n)

    Note: There is only one worksheet with multiple sheets.

    Every sheet has multiple columns (not fixed) but all of them HAVE same header name but not in the same order.

    Total number of rows in each sheet are not fixed.

    eg.:-
    Sheet1_Columns_Headers (Name, Age, ***, Roll No, Subject1, Subject2, Subject3,.....)

    Sheet2_Columns_Headers (Roll No,name,***,age,subject4,subject3,subject1,subject2,......)

    Sheet3_Columns_Headers (***,age,Roll No,name,subject4,subject3,subject1,subject2,......)
    .
    .
    .


    Output : I want all the data to be appended in Sheet1 with all the entries placed exactly below the appropriate columns or header titles.

    I would really appreciate your help as I am at a crucial stage and badly stuck at this part.

    I have also attached the worksheet.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy Data from one sheet to another using header as key

    Attachment 103002hi, nihitsk, please check attachment, run code "test".

    Notes:

    - the code sorts data on each sheet, I do not know if it's allowed or not
    - correct order of collumns is taken from Sheet1 [a1:i1]
    Attached Files Attached Files
    Last edited by watersev; 03-23-2011 at 04:20 AM.

  3. #3
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Smile Re: Copy Data from one sheet to another using header as key

    Hello Watersev,

    Thank you very much for your prompt reply.

    Well, the code is not working as it is intended to. The Rows are getting all mixed up due to sorting and the data is appearing in an inconsistent format.

    Also the data in sheet2 and sheet3 has also been changed (updated) by the code which should not have been the case.

    Please look into it once again and i am attaching the sheet again with the output I got after runnig the macro.

    Awaiting response. :-)
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy Data from one sheet to another using header as key

    check attachment
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Copy Data from one sheet to another using header as key

    Thank You Watersev. The code worked perfectly for the file I mentioned.

    i appreciate your effort.

    But I want this code to be generic.

    If the number of columns change the code does not work.. I tried modifying it but the results are haphazard.

    Can you provide me a code for this...

    Search for the first header name in the "output" sheet... compare this name with all the headers in other sheet... if a match is found.. copy all the cells underneath this matched header.. and paste below the same header in output file...

    this will make the code usable everywhere...

    And i would be finally able to put it in my module..

    Do help..

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy Data from one sheet to another using header as key

    check attachment, I've added three new columns, try it yourself
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Copy Data from one sheet to another using header as key

    Hey WaterSev,

    That code worked 99% as per required. there are still some pitfalls.

    The contents dont get copied if the output file is empty..

    And it is not possible to append the files with more than 25 headers simultaneously..

    And if the output file is non empty then the contents of the output file are redundantly copied..

    Please try to adress these issues.

    Also remove the sorting code and the things will be smoother.

    Cheers Mann...

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy Data from one sheet to another using header as key

    check attachment
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Copy Data from one sheet to another using header as key

    Hi WaterSev,

    the code works fine with the given workbook... but when i try it on anyother work book it gives a bug at tis line:

    Please Login or Register  to view this content.
    plz help me resolve this.

  10. #10
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Copy Data from one sheet to another using header as key

    Quote Originally Posted by nihitsk View Post
    Hi WaterSev,

    the code works fine with the given workbook... but when i try it on anyother work book it gives a bug at tis line:

    Please Login or Register  to view this content.
    plz help me resolve this.

    Problem Solved : Some of the columns are in the dte format...

    Is there any way we could check for dates also... or convert whole doccument into Text.

    I will try that....

    If i couldnt... this forum will surely help

  11. #11
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy Data from one sheet to another using header as key

    if the offered solution resolves your issue, please mark the thread as solved

  12. #12
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Copy Data from one sheet to another using header as key

    i dont know how to do that. Pl tell me hw to mark a thread as "Solved."

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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