+ Reply to Thread
Results 1 to 21 of 21

How to move zero-value cells to the end of a list

  1. #1
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    How to move zero-value cells to the end of a list

    Hi,

    Is there a way with either built in features or with VBA to take the items in a range and sort them so all the zero values appear at the end of the list? I am trying to take a list of text strings that have some cells in the list populated with zeros and push those zero-value cells to the end of the list without changing the order of the remaining text string cells.

    For example, I have this list in B10 through B15:

    0
    Baker
    Alpha
    0
    Zulu
    Charlie

    And I would like the B10 - B15 sort to look like:

    Baker
    Alpha
    Zulu
    Charlie
    0
    0

    Any ideas?

    Lawrence

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    =IF(B10=0,REPT("z",10),B10) drag down then sort by that column
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    Martin,

    Thanks for the reply.

    I would really REALLY like to use a helper column, but doing so would create a complication that I'd prefer avoid. And wouldn't doing the sort also change the order of the remaining text strings? Unless I could do some kind of custom sort???

    Lawrence

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    =IF(b10=0,"",ROW()) will leave them intact

  5. #5
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    MArtin,

    I guess I'm a little dense, but that converts the zeros to blanks and the list will still have those blanks spread through the list, not moved to the end. Where am I going wrong?

    Lawrence

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    Try this helper
    In C10
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    And result in D10
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Drag/Fill both Down
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    you sort by the helper select b10:c15 sort ascending by col c no headers when sorting the order is numbers then/ text then /text blanks "" so the result is
    in column b is
    Baker
    Alpha
    Zulu
    Charlie
    0
    0
    column c ends up like this(but you cant see the "")
    10
    11
    12
    13
    ""
    ""
    Attached Files Attached Files
    Last edited by martindwilson; 07-25-2012 at 04:47 AM.

  8. #8
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    if you are going down a formula route then this bit
    =IF(F10="","",IF(ISTEXT(F10),ROW(),COUNTIF(F$10:F10,0)+MATCH(LOOKUP(REPT("z",255),$B$10:$B$20),F:F,0)))
    only needs
    =IF(ISTEXT(F10),ROW(),ROW()+10^5)

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    @ Martin
    Hmm? ...

    What exactly does Column B Row 100010, 100013, 100016, etc ... contain?
    Especially if the OP might be using 2003, his/her profile doesn't say either way.

  10. #10
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    you are using small so it just moves the zeros to the end
    you end up with(it doesn't matter what version of excel its just adding a number)

    100010
    11
    12
    100013
    14
    15

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    @ Martin

    I beg to differ ...
    =INDEX(B:B,SMALL(C:C,ROWS($1:1))) using your helper will not just move the zeros to the end of the list it will knock them clean off the sheet in 2003! in later versions it will in all probability return "", instead of 0.

    As I asked, what does =INDEX(B:B,100010)) return?

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    On further thought, the solution I offered in Post #6 is incorrect ...

    My apologies.

    Adding a second proving Column to the data table.
    It should be ...
    In D10 (Helper)
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Drag/Fill Down

    Then in E10
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Drag Across to Column F then Down as required.

    See this workbook.
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How to move zero-value cells to the end of a list

    hi skysurfer, VB option without helper column
    Attached Files Attached Files

  14. #14
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to move zero-value cells to the end of a list

    by a happy coincidence anything outside the size of the workbook rows =0 =INDEX(B:B,100010)= 0 =INDEX(B:B,10000000)=0 thats in excel 97
    but since we know the length of the thing to be sorted b10:b15
    you could just use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    with (note i reversed the arguments)
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    Hmm?
    ... option without helper column
    Some b****r will soon show up with a CSE Array solution ...

  16. #16
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    You guys are amazing. Those formulaic solutions are very elegant.

    I did come across a VBA solution I would like implement (so, no helper column needed), but can't figure out one little change. It moves all the blanks to the bottom of the list. What I need it to do is move the zeros to the bottom of the list...and not not just move the VALUES but the FORMULAS.

    I tried changing the ".Value" to ".Formula", but it changed the order. Anyway, here is the code that works perfectly with blanks:

    Please Login or Register  to view this content.

  17. #17
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    @ Martin
    Nice one, I'd have sworn that would never work!

    @ skysurfer
    Two solutions, the hard way and the easy way ...
    Lucky white heather, two for the price of one ...

    Go with Martin ...

    For your, and public conveniance, see the attached.
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    Marco,

    You crack me up.

    Martin's solution is very elegant. But the ideal solution would be VBA and no helper columns. The list is formula derived and is surrounded by other data.

    I am very close to having the VBA above work. Do you know of a way to have it move the formulas instead of the values?

    Lawrence

  19. #19
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to move zero-value cells to the end of a list

    Ah ha!
    Move values and formulae, now that's a different kettle of fish!

    Time for you to post a sample workbook with these nasty little things, and pray for the VBaWaffen-snb to arrive one dark night ...

  20. #20
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    Okay, I've attached a file with both a working version of the macro and with a version that I'm trying to get to handle cells with formulas.

    (I could not send my original file because it is huge and contains sensitive sales data.)

    So, in a nut shell, I need a macro that will take items in a list (and those items are not values but formulas) and move those items that have zero value to the bottom of the list while preserving the order of the remaining items.

    This:

    Bravo
    Alpha
    0
    Charlie
    0

    Will look like this:

    Bravo
    Alpha
    Charlie
    0
    0
    Attached Files Attached Files

  21. #21
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to move zero-value cells to the end of a list

    Thanks to Andy Pope, we now have a macro that moves zeroes to the bottom of a list when the items in the list are formulas.

    (See attached)
    Attached Files Attached Files

+ 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