+ Reply to Thread
Results 1 to 15 of 15

Copy columns to a new workbook if the headers row matched

  1. #1
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Copy columns to a new workbook if the headers row matched

    Hi everyone,
    I'm very new to VBA, Macros so please forgive if there is any mistakes describing the situations.
    So basically I want to create a macro that copy columns from a Sheet, these columns have to match headers in another Sheet, to a new workbook.

    Workbook 1 has Sheet Settings (Sheet 1) and Sheet Inputfile (Sheet 2).
    Sheet Inputfile has data with columns headers for each columns.
    Sheet Settings has Column Name column, user will enter the columns that they want to copy in this column (the columns name are from Sheet Inputfile).
    I want to create a macro that copy columns from Sheet Inputfile to a new Workbook, these columns's headers have to matched the headers (in the Output line) in Sheet Settings.

    For more details you can see the attached file.

    Any help would be appreciated.
    Thank you.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    Try if like this:

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    Quote Originally Posted by Bernie Deitrick View Post
    Try if like this:

    Please Login or Register  to view this content.
    Thank you very much for the code, it helps me a lot.
    I missed a detail in the question.
    In the Output line of Sheet Settings (starts from Cell "E10"), user will place the columns in some orders (any order they want) and I want the new workbook has the columns with the same order as the user placed. Can you please help me with this?
    Again thank you very much for the help.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    Change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    and change the -1 to -9 in this line:

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 12-13-2017 at 03:03 PM.

  5. #5
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    Hi Bernie,
    Because in the Output line , the headers are on 1 row so it will be "E10", "F10", "G10", etc.. so should the code be like this instead ?
    .Range(.Range("E10"), .Cells(10, .Columns.Count).End(xlToLeft))

    And can we just copy from row 10 till last row instead of the entire column (so we dont have the blank space in the new workbook) ?

    Thank you very much.
    Last edited by haitran; 12-13-2017 at 03:47 PM.

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    Yes:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    There is a problem when I tried the code

    Please Login or Register  to view this content.
    It only shows the last column in the new workbook (column Test Name). Do you have any idea why?

    And how can I copy only from the row 10 instead of the entire column ?

    Thank you very much.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    Quote Originally Posted by haitran View Post
    Do you have any idea why?
    Because I wasn't thinking :/

    You also need to change

    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    It works now, thank you.
    Just 1 last question and I hope you don't mind. How can I copy from the row 10 instead of the entire column ?
    Thank you very much.

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    Change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    But - you don't really want to paste to an entire column when you just copy one cell. Where do you want that cell pasted? We had been using

    rngF.EntireColumn.Copy shtNew.Columns(rngC.Column - 4)

    Perhaps you should show an example of what you want (in a third sheet instead of a new workbook) based on values in those two example sheets.

  11. #11
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    Sorry to confused you.
    I want to copy from the row 10 to the last row, not just only row 10. Because from row 1 to 9 there is only blank space.

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    I understand now - just change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    That almost perfect.
    But is there any way to copy from row 10 specifically till the last row? Because sometimes in some excel files, there might be some cells that have value above the headers and I just want to get rid of them in the new workbook.
    Thank you very much.

  14. #14
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,257

    Re: Copy columns to a new workbook if the headers row matched

    That should only copy from the header down, unless the header is not in row 10:

    Please Login or Register  to view this content.
    will ensure that the copy starts at row 10 and ends at the last row (if your usedrange starts in row 1) ... Not sure where your extra values are or what is being copied that you don't want....

  15. #15
    Registered User
    Join Date
    12-10-2017
    Location
    Netherland
    MS-Off Ver
    2016
    Posts
    19

    Re: Copy columns to a new workbook if the headers row matched

    That's perfect.
    Thank you very much for the help.

+ 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. Match 2 Columns in two sheets; copy an offset Column of matched Row to Sheet 1
    By sureng20 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-01-2017, 03:08 AM
  2. [SOLVED] Copy columns of different headers
    By SBBmaster09 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-30-2016, 04:10 PM
  3. Copy columns to other worksheet with same headers
    By etaver87 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2016, 11:25 AM
  4. macro to add columns after existing columns and copy/paste headers and formulas
    By rhybeka in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2013, 02:35 PM
  5. [SOLVED] Averaging Based on Defined Row Value and Matched Column Headers
    By mcollins789 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-03-2013, 11:19 AM
  6. Search multiple workbook headers and copy column entries to another workbook
    By R3n3gad3-XtC in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-13-2012, 12:47 AM
  7. Match two Columns of two Sheets and Copy Matched Rows to a Third Sheet
    By Par in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-08-2012, 10:16 AM

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