+ Reply to Thread
Results 1 to 21 of 21

Need Macro to select and copy data, paste it to a new location, then delete the old row

  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Need Macro to select and copy data, paste it to a new location, then delete the old row

    Hi-

    I am currently trying to organize data for our research lab and would really appreciate some help. We currently have a program that generates an Excel spreadsheet of test data that we need to reorganize a bit.

    Each mouse the program monitors has a unique number, and its movement is measured through a series of trials. Unfortunately, the program creates a new row for every trial period instead of keeping each mouse's data on the same row. So instead of something useful like Animal1 - Movement Time 1- Movement Time 2.. etc in one row, the program generates a separate row for every Time Period and we end up with 7 rows of data for each mouse.

    What I would like to do is create a macro that sorts the information by Animal Number (got that part down!), checks to see if the following row is a duplicate animal number, and if it is, selects data in columns I through N, pastes the selected data on the end of the first row containing that Animal Number, and then deletes that entire row. Basically:
    1. If Animal Number = Duplicate
    2. Copy I to N of row below first original Animal Number
    3. Delete entire row containing duplicate Animal Numbers
    4. Repeat with remaining animal numbers until Animal Number cell is blank

    I have attached an example of this worksheet, the first two rows show where I manually deleted the duplicate information and pasted it onto the end of the matching Animal Number.

    We do research to alleviate Parkinson's seizures, so it's for a good cause and I would really appreciate the help!TFC Ethos 080612.xlsx

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Wow. Not only was that a super-fast reply, it works perfectly. I can't thank you enough!!

  4. #4
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    I'm sorry, but I spoke too soon. The macro doesn't seem to be deleting the top two entries after it finishes editing the list, and I cannot get it to work on larger files. The size of my data files will adjust, so is there a way to get this macro to run of files with varying record numbers? Here is the spreadsheet I'm having trouble getting it to work on.

    Thanks again for your help!
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Maybe:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    That seems to take all the data on the sheet and move it to the first row, leaving me with just one line of data The first macro was so close, I just can't figure out how to tweak those issues.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,168

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Maybe you can try this tweak to John's earlier code - (untested)
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Quote Originally Posted by summerela View Post
    That seems to take all the data on the sheet and move it to the first row, leaving me with just one line of data The first macro was so close, I just can't figure out how to tweak those issues.
    I tested it several times on the data you provided? It seemed do as you requested? Did you run it on the test file "Ethos..."? One assumption I made is that the Animal numbers had a consistent number of rows. In your first sample it was four. In the second sample it was 16.
    Last edited by JOHN H. DAVIS; 09-25-2012 at 01:00 PM.

  9. #9
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Hi John-

    Each Animal Number should have 5 rows of data, one for each trial period; NT1, NT2, NT3, TI and T2. I see now that when I copied and pasted the data from our excel spreadsheet into the one I posted, it changed the formatting of the time periods (no clue why or how it did that) thus making it appear that each Animal Number has 10 rows of data instead of 5. I did not catch this error before, I'm really sorry about that!

    I re-uploaded the file exactly the way it should look at the point where we will need the macro. It's SO CLOSE to working!
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Maybe:

    Please Login or Register  to view this content.

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    see if this will work?

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  12. #12
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Heres an update to post #11. Was not getting all fields. Also code does not rely on Animal Numbers being sorted A-Z

    Please Login or Register  to view this content.
    Last edited by mike7952; 09-25-2012 at 04:26 PM.

  13. #13
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Hi Mike-

    I was still trying to get your previous macro to work, sorry for the delay in reply. Using your most updated macro, there should be six columns of data moved from each row, so NT1 has seven values (Total Distance Moved, Distribution, Duration, Frequency, Latency to First, Moving, Not Moving), NT2 has seven values, NT3 has seven, etc. However it only appears to be copying over 4, skipping a column, copying 5, skipping a column, etc.

  14. #14
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Oh- Just saw your update. Now I am not getting the empty columns, but am still missing several columns. I'm so glad you're out there. This has been driving me up the wall for two days now!!

  15. #15
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Your wanting to Add columns H to M to the row right?

  16. #16
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    I also noticed from your different workbook examples Animal numbers start in column C and another starts in column D?
    When you say missing several columns have tested this on your lastest workbook you uploaded or a different. I looked at the data and its correct with the workbook you uploaded.

  17. #17
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Six values, not seven. I need coffee. But yes, adding values from columns H to M.

  18. #18
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    I'm using the same workbook I uploaded for testing. Where the value of Animal Number is in column C. There seem to be six columns missing, I'm not being as thorough in checking this as I don't want to make you wait long for my reply.

  19. #19
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    TEST (2).xlsm

    Look over the attachment I see all column. I colored coded and numbered to data sets. Compare Analysis Original with Analysis and let me know what Im missing

  20. #20
    Registered User
    Join Date
    08-16-2012
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Ok, I went through and deleted all the macros I had stored in my personal workbook and tried it again. I think I was attempting to use not your latest, but the one JUST before. It works. OMG it works!!!!!!

    Thank you so much, I am so grateful!! You rock! Let's mark this Solved, shall we?

  21. #21
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Need Macro to select and copy data, paste it to a new location, then delete the old ro

    Good Im glad it works for you. Just so you know the code does not rely on the data being sorted or the amount of of test per animal. So you could have 5 test like your latest workbok di or it could be 10 like your other workbook did, it wont matter. Also remember its looking for Animal Nums in Column C.

+ 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