+ Reply to Thread
Results 1 to 9 of 9

VBA for Sorting Rows

  1. #1
    Registered User
    Join Date
    09-08-2010
    Location
    Portsmouth
    MS-Off Ver
    Excel 2003
    Posts
    23

    VBA for Sorting Rows

    Hi all,

    have come across this forum as been looking for VBA for a spreadsheet I have created.

    The table has 3 columns A- description B- status and C- date status updated. I have created a drop down in column B and have stored a VBA so that when one of 6 drop downs is selected, the row will change colour. Now what I am looking to do is sort the rows automatically so that when a drop down is selected the whole row is moved with other rows of that status and then by date order in that status. I have created my own sort as I want them to stay in order and not go into alphabetical order.

    Could anybody help with this please? I have attached a copy of the spreadsheet so far.

    Something else I am possibly looking to do (although may be getting too complicated) is add a final drop down 'completed' and when a row is changed to this, I would like the row removed and placed onto the 2nd worksheet which shows completed jobs.

    Thanks in advance. Hope I explained myself ok

    Dan
    Attached Files Attached Files

  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
    48,290

    Re: VBA for Sorting Rows

    You have explained your requirement very well. However, I don't quite understand why you use a worksheet change event to colour the rows ... why not use simple conditional formatting?

    If you do feel the need to use VBA, I would restrict the range that is being checked to just column B. Otherwise, if you edit something in column A, the colour will change.

    And, as you already have the worksheet change event, you could just tag a call to your sort code onto the end of that. But ... your users might find it confusing if the row they have changed moves somewhere else on the worksheet ... particularly if there are a lot of rows of data.

    Regards

  3. #3
    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: VBA for Sorting Rows

    Hello DJ_Rutts,

    This a was tricky problem. Everything works for changing the colors and sorting the group chosen by the status. I didn't get to your second request due to time constraints. Here is the macro that has been added to the attached workbook.

    Worksheet "Outstanding" Change Event Code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    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!)

  4. #4
    Registered User
    Join Date
    09-08-2010
    Location
    Portsmouth
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: VBA for Sorting Rows

    Hi, I couldn't use basic conditional formatting as there are more than 4 colours.
    Thanks very much for your help Leith, its looking much better! Only thing is, that I would each status to stay in the same order it is in the drop down, rather than move to the top when it is selected. So starting with all the 'Awaiting CR Review' at the top and ending with all the 'Awaiting Implemenation' at the bottom, with each status staying in date order.

    The second thing I was speaking of, was adding a 7th drop down to the list being 'Completed' and when this was selected the row could then be removed from the outstanding spreadsheet and the decription added to the completed work sheet which is the 2nd tab. The dates on the completed worksheet could be completed manually but this would save jobs being missed off, or having to copy and paste data across. Is that possible?

  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
    48,290

    Re: VBA for Sorting Rows

    If you are using Excel 2007 or above, I believe the number of conditions is limited only by memory.

    A little less code and a little more external flexibility.

    See attached screenshot.

    Regards
    Attached Images Attached Images

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

    Re: VBA for Sorting Rows

    You probably already found this yourself:

    Please Login or Register  to view this content.



  7. #7
    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: VBA for Sorting Rows

    Hello DJ_Rutts,

    Sorry for the late reply. I had to drop my car off for repairs this morning. The macro will now sort the "Outstanding" information first by the status list order and then by date in ascending order. This is done by creating a Custom Sort List. Here are the macros and the updated workbook.

    Module1 Macros
    Please Login or Register  to view this content.

    Outstanding Worksheet_Change Event Code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    09-08-2010
    Location
    Portsmouth
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: VBA for Sorting Rows

    Sorry, I am probably becoming a bit of a pain now with this! Unfortunately that macro to sort the list does not seem to work quite as it should for some reason and I cannot see why. The order it is placing the list in is: Awaiting CR review, Development, System testing, User testing, Awaiting Implementation, awaiting Auth and awaiting est - rather than the order of the drop down. Any ideas on why this is?

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

    Re: VBA for Sorting Rows

    If you sort the list in column A in sheet "list", you wil see the same order..

+ 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