+ Reply to Thread
Results 1 to 13 of 13

Sort a selection by last column - or sort a range from column B onwards to last empty cell

  1. #1
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33

    Sort a selection by last column - or sort a range from column B onwards to last empty cell

    Hi Group,
    I hope you don't my me asking this as I've spent hours trying to solve this and you might know a quick and easy way. I've scoured Google and I've visited nearly every link that comes up on page, so have tried my best.

    I have become good at finding a macro and adjusting but this solution is tricky and I can't seem to solve it.

    I the spreadsheet in the attached image.

    What I want to achieve is select B8:J25, and sort on the last column that contains a value (in the example, this is column J25).

    The problem is, each month, I will add another 2 columns, so next month I will need to sort B8:L25 etc.

    If I record a Macro, I get this:


    Please Login or Register  to view this content.
    What I ideally would like to be able to do is the same, but the part in red to be dynamic, so that the macro selects cell B8, and then does something like:

    Please Login or Register  to view this content.
    and then runs the first macro, but with the dynamic column. One thing that is constant is that rows B8:B25 will always be the vertical selection - it is just the horizontal one I am stuck with.

    Any help greatly appreciated, my brain is going into meltdown.

    (Just updated my post with the code tags, apologies).

    Best Regards,
    Paul


    screenshot.jpg
    Attached Images Attached Images
    Last edited by atombomb99; 09-07-2018 at 12:37 PM.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,795

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    try
    Please Login or Register  to view this content.
    Also when posting code please use code tags, the # icon in the reply window.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Try this:

    Please Login or Register  to view this content.
    Last edited by xladept; 09-07-2018 at 12:52 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Hi Both, many thanks. I tried both solutions and the second one was shorter code but I think might get stuck because the top row are merged cells (I inherited the spreadsheet) as it gives a merged cell error. I tried Fluff13's solution and am dissecting how it works but it seems to have done the trick.

    Wow, you are all amazing, thankyou for the fast replies within almost minutes, after literally hours of trying to get this to work.

    All the best,
    Paul

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,795

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Glad we could help & thanks for the feedback

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    You're welcome

    Row 8 is merged cells or is it Row 7??

  7. #7
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Hi XLADept,
    The rows above B8 (B5:b7 are merged vertically) and C5:D5 merged horizontally (The headings and month ones basically).

    I am trying to dissect both bits of code to understand and learn from you both

    Best Regards,
    Paul

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Redefined the sort range - hope it works

    Please Login or Register  to view this content.
    BTW - Thanks for the rep!

  9. #9
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Hi xladept,
    I tried that one but I get that "Run-time error '1004': To do this, all merged cells need to be the same size."

    Best Regards,
    Paul

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Is this how you wanted?
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Hi everyone :D I got it working with the first solution but I always like getting things working when they should but hit a snag, so just uploading the sample spreadsheet just in case it helps with a link.


    Please Login or Register  to view this content.
    Apologies for the garbled code hash link, it would let me post the prefixed version as I haven't posted enough yet.

    All the best,
    Paul

  12. #12
    Registered User
    Join Date
    05-30-2013
    Location
    England
    MS-Off Ver
    Excel Mac V16.68 and Excel 2019
    Posts
    33
    Quote Originally Posted by jindon View Post
    Is this how you wanted?
    Please Login or Register  to view this content.
    Hi Jindon,
    Wow I’ve never even seen the intersect command, going to look it up. I pasted it in and it seemed to sort something but not the last column.

    Regards,
    Paul

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sort a selection by last column - or sort a range from column B onwards to last empty

    Quote Originally Posted by atombomb99 View Post
    I pasted it in and it seemed to sort something but not the last column.
    It is working here, sort by the last column.
    I need to see your workbook, if it is not working at your end.



    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

+ 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: 03-29-2018, 07:58 AM
  2. Sort and move to another empty cell in column
    By shimaa01234 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-14-2014, 07:00 PM
  3. Sort Select Range: Either Find Column Header or Last Column:Last Row
    By mbryson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-02-2014, 08:03 AM
  4. Sort column, new sort after each blank cell
    By mike_m1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-19-2014, 03:51 PM
  5. Replies: 2
    Last Post: 01-15-2014, 09:31 AM
  6. [SOLVED] sort a column with expanded selection - issue with merged cells.
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2013, 11:33 AM
  7. [SOLVED] excel change default column sort to current selection
    By john palmer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-08-2005, 12:06 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