+ Reply to Thread
Results 1 to 11 of 11

Sorting with VBA

  1. #1
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Sorting with VBA

    Hello,

    I use the code below to sort my data in a sheet. It kind of works but like to be able to sort it better.
    If you look at the attached image I'd like all rows with empty cells to end up at the bottom of the table but can't seem to get this to work.
    Does anybody have a good suggestion for doing that?2016-12-23 15_55_54-100m [reparerad] - Excel.jpg


    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Re: Sorting with VBA

    This is a better pictures with columns also showing

    Attachment 494610

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Sorting with VBA

    .
    .
    If there will always be some data in the last column to the right, this code should work:

    Please Login or Register  to view this content.
    You can change the Range spec as required.
    Attached Files Attached Files

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Sorting with VBA

    And this will sort on Col A
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Re: Sorting with VBA

    Thanks for your answer. I think I should have added some more information to begin with though. The way I want it to work is this:

    1. Column L will not always contain information and the rows that do contain information are the ones I need at the top of the table.
    2. Column K contains a random value so that the rows are sorted on top but not always in the same order
    3. The thing I'm missing is that I'd then like to have all rows containting some information but no text in column L appearing before all rows that are completely empty.

    Sorry for not providing this info to begin with.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sorting with VBA

    Perhaps


    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Re: Sorting with VBA

    I also tried this, but it still does not sort it like I want it to:
    (it still sorts like the screenshots I attached earlier)

    Please Login or Register  to view this content.

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Sorting with VBA

    Help us understand more specifically ... you are sorting on Col L ? Cuz it will always have some kind of data in it ? That would be a strong key to go with, which Col always has data.

  9. #9
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Re: Sorting with VBA

    Now I feel kind of stupid because I actually solved it myself with this code.
    I just changed the order of the keys and things work like I want them to.

    Columns("C:L").Sort key1:=Range("L2"), key2:=Range("F2"), key3:=Range("K2"), _
    order1:=xlAscending, Header:=xlYes

    Thanks for your attention and trying to help me

  10. #10
    Registered User
    Join Date
    10-23-2008
    Location
    Sweden
    Posts
    92

    Re: Sorting with VBA

    Now I feel kind of stupid because I actually solved it myself with this code.
    I just changed the order of the keys and things work like I want them to.

    Please Login or Register  to view this content.
    Thanks for your attention and trying to help me

  11. #11
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Sorting with VBA

    Great ! It's a good feeling when you figure it out.

    Merry Christmas !

    ps: Love your name. Reminds me of the singer John Denver

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 12-16-2015, 01:04 PM
  2. [SOLVED] I require sorting formula in excel sheet with complex sorting.
    By kaminanirav in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-24-2015, 08:16 AM
  3. Replies: 0
    Last Post: 02-24-2014, 11:27 AM
  4. Sorting 2 data ranges by comparing one column in each and sorting to match
    By MDKsmiffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-17-2013, 03:30 PM
  5. Sorting a range to invert it upside down without sorting by any specific column
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-29-2013, 12:53 PM
  6. [SOLVED] Sorting a range to invert it upside down without sorting by any specific column
    By luv2glyd in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-28-2013, 12:43 PM

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