+ Reply to Thread
Results 1 to 10 of 10

How to sort data with macro or VBA

  1. #1
    Forum Contributor
    Join Date
    12-07-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Unhappy How to sort data with macro or VBA

    Dear All,

    I am getting a file on daily basis with 10.000 row and 20 different column and everymorning i have to sort the date using the filter option on excel, cos i dont need all the rows and columns on the file. is there any way i can set up macro or VBA that it can select the data and the category of my choice .
    I attached a sample and marked columns and rows with yellow colour which the data i need from this file

    Could anyone help, I really need this to make my life easier
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    05-01-2009
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    26

    Lightbulb Re: How to sort data with macro or VBA

    Figuring out Macro's by simply recording your actions and reviewing them afterwards is probably the best way to learn. Have you tried pressing the "Record Macro"-button (bottom-left corner of the Excel screen)? Try to record your Macro, then review it by clicking the Developer-tab (upper-right tab in Excel).

    If that gets too complex for you,

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    12-07-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Lightbulb Re: How to sort data with macro or VBA

    Hi bmmerkx
    Thanks for the reply,
    I can record a macro very basic ones, i can create based on todays data, but when i get new file tomorrow, i paste row data into macro file and when i click run macro, it does get messey and doesnt pcik up the data i want.
    Last edited by shg; 04-16-2010 at 07:10 PM. Reason: deleted spurious quote

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to sort data with macro or VBA

    Hello Benjamin2008,

    I see the columns you want to keep. The rows look like column "E", Document Type, is used to determine what will be kept. Is this the complete list? Will this list change?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    12-07-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: How to sort data with macro or VBA

    Quote Originally Posted by Leith Ross View Post
    Hello Benjamin2008,

    I see the columns you want to keep. The rows look like column "E", Document Type, is used to determine what will be kept. Is this the complete list? Will this list change?
    Hi Leith Ross

    Thanks for the reply,
    yes this is the final complete list, so nothing will be changed or deleted or added

    Hope you be able to show me away.
    many thanks once again
    Benjamin

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to sort data with macro or VBA

    Hello Benajmin2008,

    Sorry for the late response. This was a tricky problem. The biggest issue I had with writing the macro was execution speed. I tried many different ways and this version gave me the best performance. Add this macro to a separate VBA module in your workbook. I am still unable to upload files.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: How to sort data with macro or VBA

    hi all,

    Quote Originally Posted by Leith Ross View Post
    ... The biggest issue I had with writing the macro was execution speed. I tried many different ways and this version gave me the best performance...
    Leith,
    I'm curious, what were the different ways that you tried & how did they time/rank in terms of performance?

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to sort data with macro or VBA

    Hello Rob,

    To make a long story short, I originally planned on clearing the data rows that didn't match, sort the rows, and use SpecialCells to delete the blanks. The big obstacle was the top 7 rows contain merged cells. Even with the range defined as only the data in the table, Excel would throw an error saying that merged cells must all be the same size. To get around that issue the data needed to be copied without those rows for sorting, filtering, etc. The largest amount of time is spent deleting the unneeded columns. This proved faster that assembling only the data for each row with only the cells of interest and copying them over.

    The timing was measured by placing a a variable at the beginning of the Sub to hold the Timer value, and another at the end of the Sub to hold the timer value at that point. Subtracting the two variables gives me the total execution time in microseconds.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to sort data with macro or VBA

    Hello Benjamin20008,

    The speed issue was still bothering me and I worked on the macro some more. This version ran in 23 seconds on 12,000 lines of data. I used the data in your workbook and added some full lines and then replicated it to fill 12000 rows. Instead of deleting the columns, I hid them. This saved a lot of time. I can live with this speed. Here is the improved version...
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: How to sort data with macro or VBA

    hi Leith,

    Ahhh, merged cells rear their ugly heads again!
    Oh well, with your use of DocType you've bypassed the potential issue related to the limitation of specialcells (see Ron DeBruin's site) .

    Rob

+ 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