+ Reply to Thread
Results 1 to 17 of 17

VBA Excel: Copy/Paste a column range to another sheet IF column headers match

  1. #1
    Registered User
    Join Date
    10-26-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    I am building a performance appraisal document. The sheet "Sheet1" acts like a form where users will select the current month from a dropdown box (M5) and then fill out their results in the cells below (M6:Mxx).

    Another sheet, "Sheet2" is a log sheet. Cells D7:I7 are column headers and each cell is a month name(i.e. d7=July, e7=August). These headers are in the same format as the dropdown in the Monthly Template sheet.

    When the user clicks a Command Button, I want the sheet to match the value in "Sheet1(M5)" to "Sheet2(D7:I7)" and paste the range of results (Sheet1(M6:Mxx) below the matching value in Sheet2

    i.e. if Sheet1M5 = July and Sheet2D7 = July, copy range from M6 down to the first empty cell, into Sheet2 D8 onwards.

    Could I please have your help to write the code that will achieve this?

    Thanks Hayley

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Something like:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    10-26-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Hi TMS,

    Thanks. With the line

    Sheet2.Range("A1:O1"), _

    Can you explain why I would use this range when the fields I want to match are in D7:i7?

    Thanks
    Hayley

  4. #4
    Registered User
    Join Date
    10-26-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Quote Originally Posted by TMS View Post
    Something like:

    Please Login or Register  to view this content.

    I'm getting a runtime error 1004 : Unable to match property of Worksheet Function

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    I screwed up . Because I mis-read the question and assumed the headers were in row 1, Should have been D7:I7. However, I start with A7 because I want to get a column number, not an index relative to D7, and go to O7 to allow 12 months.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Hi Hayley (and fellow Aussie ),

    Here's my attempt:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    And here's my next attempt using some of TMS's nifty code:

    Please Login or Register  to view this content.
    Regards,

    Robert

  8. #8
    Registered User
    Join Date
    10-26-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Quote Originally Posted by Trebor76 View Post
    And here's my next attempt using some of TMS's nifty code:

    Please Login or Register  to view this content.
    Regards,

    Robert

    Hey Robert, it is pasting to the new sheet but is going into column A and B? It also seems to be copying information below the first blank cell it encounters?

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    This should do everything ... I also missed the requirement for "next available row" so that's fixed here. Commented so you can adapt it if necessary:

    Please Login or Register  to view this content.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Robert's code is matching against D7:O7 and therefore returning an index relative to that range. As I said previously, that index would need to be modified for it to be a valid column reference ... and I can't see that has been done.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Just to clarify, if you input data for January, it will match "January" against D7:O7 and find "January" in cell D7, the first cell of the range, and return 1.

    It then uses 1 as the column reference and copies the data to column 1, that is column A. May's data would be posted under February.

  12. #12
    Registered User
    Join Date
    10-26-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Thanks again for your assistance. I am however getting a "Runtime error 9 - subscript out of range" error here
    lColumn = Application.WorksheetFunction.Match( _
    Sheet1.Range("M5"), _
    .Range("A7:O7"), _
    0)

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Post your workbook, or a sample with the input and output.

    I use the headings on Sheet 2 as the Data Validation List on Sheet 1 so there should not be a subscript out of range.

    Could mean you have different spellings or trailing spaces, etc

  14. #14
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Hey Robert, it is pasting to the new sheet but is going into column A and B?
    Change this line of code...

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

    Please Login or Register  to view this content.
    It also seems to be copying information below the first blank cell it encounters?
    Not sure what you mean by this. It should paste to the next available row in the desired column in Sheet2. Change the code as I've outlined above and try it again - hopefully all will be good.

    Robert

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Please Login or Register  to view this content.
    I like it, heh, heh. Don't know if it has any efficiency ramifications but I doubt it will be an issue.

  16. #16
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Yeah looking across 16,384 columns probably isn't most efficient way but it's just a once off so shouldn't be an issue. I've seen plenty of MATCH code referencing a whole column so that's 1,048,576 rows and it seems to run OK.

    Glad you liked it

    Cheers,

    Robert

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,430

    Re: VBA Excel: Copy/Paste a column range to another sheet IF column headers match

    Glad you liked it
    Made me laugh

+ 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. copy and paste columns based on column headers
    By patwary786 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-24-2015, 11:43 PM
  2. Replies: 1
    Last Post: 05-30-2015, 04:38 PM
  3. Replies: 3
    Last Post: 11-06-2014, 01:23 AM
  4. Replies: 2
    Last Post: 01-22-2014, 05:36 PM
  5. [SOLVED] Match value(s) in column with headers of another sheet and paste a cell value from column
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-17-2013, 11:34 AM
  6. [SOLVED] Match, Copy and Paste Column Headers - varying number of columns
    By itcher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-19-2013, 10:49 AM
  7. [SOLVED] VBA code to copy column data only if headers match
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2012, 11:23 PM

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