+ Reply to Thread
Results 1 to 11 of 11

Flatten Data Script

  1. #1
    Forum Contributor
    Join Date
    02-14-2007
    Location
    Chester, England
    MS-Off Ver
    2010 & 2016
    Posts
    312

    Flatten Data Script

    Hi All

    I have written a small bit of script which loops through data. It assumes that the data is sorted by employee. If there is more than one record for the same employee it will place certain fields onto the next available column for the top row of data for that employee (effectively flattening out history records on to one row)

    The code does what I want but is intended to be run on a data set of > 300,000 records. When I have tried this it takes ages (I stopped it after 15 minutes and it had only done 14000 records).

    I have tried to re-write this using "for, each, next" instead of activating each cell but I couldn't work out how to include the "If" and "Do While"

    I have attached a sheet with the script and some sample records. I would really appreciate it if anyone could give me any pointers to speed this code up.

    Thanks in advance

    Regards

    Jim
    Attached Files Attached Files
    Last edited by JimmyA; 11-15-2011 at 12:09 PM.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Flatten Data Script

    Hi,

    Activating slows down the code. Also if you delete rows then you need to start at the bottom and work your way up.

    Here is a modified code (I think this is what you are after):

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

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Flatten Data Script

    Instead of reading/writing/adapting the sheet this code puts all results into an array.
    Only when the code is finished the results will be written into the sheet.
    The result will be in the first empty column after the currentregion.
    Texttocolumn will distribute the result into separate columns.

    Please Login or Register  to view this content.



  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Flatten Data Script

    snb, I like your approach and I am wondering how much memory this will absorb since everything is being saved in the RAM until the code is completed. I will try to test this will variable rows and see how the performance does on larger and larger sets.

    Thanks for sharing this approach.

    abousetta

  5. #5
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Flatten Data Script

    Hi,

    I have made some changes to the code, see if it's faster

    Steffen Thomsen
    Attached Files Attached Files

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Flatten Data Script

    The maximum number of elements in the array is limited by available memory or the Excel worksheet maximum size (65536 rows X 256 columns). However, the maximum number of elements in the array that you can pass to Excel using the Excel Transpose function is 5461.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Flatten Data Script

    Pike, that is really interesting. I didn't know about this limitation to transpose in vba. I am wondering what the workaround to this limition would be for large sets like the one that the OP mentioned (>300,000 rows)?

    abousetta

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Flatten Data Script

    possibly use "For Each in"

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Flatten Data Script

    @abousetta

    In my suggestion I use transpose only in a horizontal manner. So the limitation of transpose needs no 'workaround' since I assume the OP hasn't more than 5160 columns to deal with.

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Flatten Data Script

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

  11. #11
    Forum Contributor
    Join Date
    02-14-2007
    Location
    Chester, England
    MS-Off Ver
    2010 & 2016
    Posts
    312

    Re: Flatten Data Script

    Wow! Thank you all very much for your replies! It's really appreciated.

    Some of the scripts are way beyond my current level of vba but I will try and understand them to improve my skills.

    Thanks again

    Jim

+ 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