+ Reply to Thread
Results 1 to 9 of 9

Copying concatenating two rows in a macro

  1. #1
    Registered User
    Join Date
    04-20-2009
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2003
    Posts
    5

    Copying concatenating two rows in a macro

    Alright, this question sounds simple at first, but nothing I've tried all day will do what I want it to.
    I've attached my sample workbook with my macro so you can see my data

    My workbook has two sheets. sheet1 and sheet2.

    My macro needs to compare column A from sheet2 with column A in sheet1. Anytime there is a match I want to take the row that matched on sheet2 and paste it on the end of the corresponding row in sheet1.

    I believe I have the nested for loops that are doing the comparison working properly, what I can't figure out is how to copy the row from sheet2 and paste it at the end of the corresponding row in sheet1. It needs to be dynamic, the row lengths may differ and ideally I would like to exclude the cell in column A, sheet2 when I paste that row into sheet1, however this isn't required.

    Any help is appreciated.

    Here is the macro, it is also included in the attachment.

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

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Copying concatenating two rows in a macro

    Hi

    See how this goes.

    Please Login or Register  to view this content.
    rylo

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copying concatenating two rows in a macro

    I just did my own version:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    04-20-2009
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying concatenating two rows in a macro

    You guys are amazing. Both versions work flawlessly.

    Thanks a bunch!

  5. #5
    Registered User
    Join Date
    04-20-2009
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying concatenating two rows in a macro

    Whoops, spoke a little too soon. JBeauclaire, your version is pasting the matched rows one row higher than it should be.

    Aka: It matches B with B properly, then pastes the result on sheet 1 in the A row instead of B.

    On another note, if the number of columns of both these sheets were any different than the sample, how could we adjust the macro to account for this? A new workbook may have more columns than the sample workbook on either sheet or both.

    In rylo's example I can easily adjust
    Please Login or Register  to view this content.
    for any header values, but I'm trying to disect the findit portion of the macro to figure out what could make it dynamically account for more columns, or less. I believe it already is accounting for more or less rows, which is great.
    Last edited by VBA Noob; 04-21-2009 at 05:54 PM.

  6. #6
    Registered User
    Join Date
    04-20-2009
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying concatenating two rows in a macro

    I think I figured it out.

    I took this line
    Please Login or Register  to view this content.
    And depending on my worksheet I can manually change those values. (0,4) can be (0,6) if I want the data copied in column 7, and (1,3) can be (1,4) if there is one more column of data in sheet2 for example.

    Thanks again for the help.
    Last edited by VBA Noob; 04-21-2009 at 05:54 PM.

  7. #7
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Copying concatenating two rows in a macro

    Hi

    You have got it on the sizing. However, if there is some consistent way of determining just what you want changed, where it starts / ends, or how many columns are to be included, it may be possible to have those items adjusted dynamically.

    Is that sort of detail able to be determined from your data?

    rylo

  8. #8
    Registered User
    Join Date
    04-20-2009
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Copying concatenating two rows in a macro

    Well there will always be 2 sheets. The columns will always start in column A on both sheets. The final column on both sheets will be variable.

    In every case, all columns in sheet2 will be appended to the columns in sheet 1 (for the particular row that is getting pasted from sheet 2 to sheet 1)

    So really just accounting for more or less columns in either sheet. I figured it would be similar to the way that the macro already adjusts for more or less rows when determining the data set.

  9. #9
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Copying concatenating two rows in a macro

    Hi

    Have a go at adjusting the code and see if you can make it work. Come back if you have problems.

    Have fun...

    rylo

+ 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