+ Reply to Thread
Results 1 to 20 of 20

How to Sort Rows by dynamically determining first and last row

  1. #1
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    How to Sort Rows by dynamically determining first and last row

    I have a worksheet that has a few rows of header information. I'd like to give those rows a name of "DashboardHDR".
    I'd like to locate the FIRST ROW after that named range of rows.
    Then I'd like to locate the final row by looking in column B for BLANKS and then the previous row is the LAST ROW.
    Then I'd like to sort from FIRST ROW to LAST ROW, ascending, on the value in column F.

    Bonus Question: Column F header value is "Work Type". The last row in "DashboardHDR" contains the header text. Would it be possible to designate the sort column by locating the column associated with header text = "Work Type"?

    I have to give full disclosure ... I am not an Excel programmer. I am a programmer (C, assembler/mainframe, pltpf/sabrtalk, VBA). I've done some VBA coding for Access, but fully self-taught. So, I have a little knowledge and know what I want to do ... a dangerous combination.

    My goal: create a macro and a button on the worksheet to sort the detail rows on the sheet by a particular column, regardless of how many rows of detail data there is.

    Thanks, in advance, for any assistance.
    Last edited by slbrick; 03-31-2017 at 12:08 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hi,

    It should be fairly trivial to get a macro to identify two Long (Integer) variables that identify the first and last row numbers to be included in the sort and a third variable to identify the Work Type column. Then armed with the 3 variables a simple Sort line of code will achieve what you want.

    For a more specific response we'll need to see the workbook.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Ok. Great. Here (attached) is my spreadsheet. As you will see the 'Dashboard' and 'Additional Info' tabs already
    have buttons with macros assigned to do various sorts. The problem is that the row ranges are hard coded in each macro.
    My goal is to make the sort actions dynamic; that is the macro will determine how many rows based on the criteria I initially stated:

    1. First Row is the row immediately following the header rows (Range Name: "DashboardHDR")
    2. Last Row is the row immediately preceding the row that has BLANKS in the cell with Column Header Text "Tab Name".
    (Column B)

    Once I see it done for the Dashboard I think I'll be able to figure it out for tab 'Additional Info'. Perhaps I'll be
    able to make the macros usable across worksheets (that is use the same sort macros for 'Dashboard' tab and
    'Additional Info' tab.

    Well .. good luck and THANK YOU!!!
    Last edited by slbrick; 04-05-2017 at 12:39 PM.

  4. #4
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    I thought I replied to your reply but I think I replied to the original thread. The attachment is in there. If you need me to re-reply to your reply (is this a comedy act?) ... let me know. Thanks.

    Sue

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Here's one approach

    Point the button on the Dashboard to the SortDashboard procedure. This passes the VB name of the sheet to the SortSheets Procedure. For other sheets create other Procedures similar to the SortDashboard procedure.


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    Just an observation and unrelated to your request but I note that you are using merged cells. Many of us avoid these like the plague since they can cause problems with other aspects of Excel and VBA code and with more complex workbooks one day you'll find these trip you up.

  6. #6
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Thank you, Richard. I was away for a couple of days. Now that I see your reply, I'll get on the fix and respond with the results.
    Also, thanks for your observation and recommendation. I'm always trying to improve and appreciate all the help I can get.

    Sue

  7. #7
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Richard,
    Thank you for the help. I am almost there. I tweaked the SortSheets routine, that you provided, by adding another input argument, enabling it to be called for a variety of sorts (sorting on different columns). And since there is no row containing the text "Projects Removed" on the "Additional Info" tab I am attempting to search for the first row with blanks in the cells. This is not working yet. I'm getting the following error "Runtime Error '13', Type Mismatch" on the following statement:

    Please Login or Register  to view this content.
    Do you have any ideas about this error? Here is the entire routine and how it will be called (the calling routines).

    Please Login or Register  to view this content.

    Thanks.
    Regards,
    Sue
    Last edited by slbrick; 04-03-2017 at 01:35 PM.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hello Sue,

    Just dashing out for the evening but you appear to be trying to find a space character " " as opposed to presumably a completely blank cell.
    Try instead a Null i.e. ""

    If you're still struggling upload the workbook and I'll take a look later.

  9. #9
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Hi, Richard.

    Well, I'm really stuck on trying to find the first blank line. Here's the routine with the statement that is failing in red:
    I am trying to locate the last line of the list (rows) of projects. So, I would like to search for the first 'blank' row.
    Take a look at the 'Dashboard' tab in the workbook attached. You will see that there are two "tables" of information and I would like to sort the detail lines of only the first 'table'.

    If you have a suggestion, please let me know.

    Please Login or Register  to view this content.
    Last edited by slbrick; 04-05-2017 at 12:40 PM.

  10. #10
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hi,

    In that case and assuming there is always a contiguous series of cell in column A below the lFirstRow variable follwed by a blank cell the I'd be inclined to use

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


    instead of the .Find syntax

  11. #11
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Thank you for the assistance, but it's not exactly what I need.

    The 'Dashboard' tab (Sheet1) has a "bunch of rows" populated then it has a blank row and then it has another 'bunch of rows' populated.
    I'd like to row number of the single blank row. Then subtract 1 to determine the LastRow for the sort action.
    That row number should be the value of lLastRow.

    Just for testing, I've hard coded a value for the LastRow and continued to test and the attempt to determine the column to be sorted on is failing with the following error:

    "Run-time error '91':
    Object variable or With block variable not set"

    Here's the routine with the line that is failing in red:

    Please Login or Register  to view this content.

    Any help would be appreciated.

    Thanks, much.
    Regards,
    Sue
    Last edited by slbrick; 04-03-2017 at 11:58 PM.

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    The lSortColumn =.... line works for me. What's the value of sColTxt when it Debugs?

    I'd still be inclined to use the following to find the last row (given the caveats I mentioned earlier) rather than hard coding the variable.
    lLastRow = sh.Cells(lFirstRow, "A").End(xlDown).Row

  13. #13
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Hi. sColTxt has a value of "Work Type".

    While in Debug ... hovering over the variable definition in the sub( ... ) line shows

    sColTxt = "Work Type"

    This is what I would expect, when clicking on the 'Sort by Work Type' button on the "Additional Info" tab. (note: I can't cut and paste from the actual running of the workbook because I'm running it on my work laptop and writing to you on my home PC).


    Stepping thru the instructions/statements while in Debug, when I get to the attempt to populate lSortColumn (when using the hardcoded instructions to set lLastRow) the instruction is:

    Please Login or Register  to view this content.
    I get the following error:

    Run-time error '91':
    Object variable or With block variable not set


    When I use the following to attempt to populate lLastRow:

    Please Login or Register  to view this content.
    I get the following error:

    Run-time error '1004':
    Application-defined or object-defined error


    I'll attach a copy of the workbook with all of the variations for populating these variables (commented out lines that I keep commenting or uncommenting for testing/debugging).
    You will find the code in Module2.

    To say that this is very frustrating is putting it mildly. (I love programming. I love programming. I love programming. ... I say red-faced).

    Thanks for your assistance.
    Regards,
    Sue
    Last edited by slbrick; 04-05-2017 at 12:41 PM.

  14. #14
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hi,

    It would appear that the .End(xlDown) syntax won't work after a .Cells instruction. Try instead

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

  15. #15
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Richard,
    I wish I had good news. But ... it didn't work. Using the Range instead of Cells

    Please Login or Register  to view this content.
    resulted in the following error:

    Run-time error '1004':
    Application-defined or object-defined error


    I'll keep searching for a solution. I just know that this can be done.
    If you or anybody else has any ideas, please feel free to respond to this thread.

    Thanks, much.
    Regards,
    Sue

  16. #16
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hi,

    Would you upload the latest workbook you're using so that I can see the error in context.

  17. #17
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Richard,

    Here (attached) it is, the current workbook.

    Before I go ... I have a question:

    Is it possible that after the first two 'finds' of text "Tab Name" that the focus has been put on that specific cell and that I need to reset the "focus" (selected cell or area) or perhaps select the rows on the sheet after the row that contains text "Tab Name"?

    Thanks, again, for your assistance.

    Regards,
    Sue
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    HOLY COW!!! I got it to work. Admittedly, I did use an inelegant approach, but at this point I must move on to actually attending to my other work responsibilities. I'll embed some of the code, below. I won't mark this as solved quite yet so as to get the opinions of some of you experts. After 1 or 2 comments, then I'll mark this as solved.

    Here are some of the problems that I ran into along the way:
    1. Wanted to have a sort routine that could be called from different worksheets
    2. Wanted to dynamically determine the first row of the area to be sorted
    3. Wanted to dynamically determine the last row of the area to be sorted
    4. Wanted to let the caller of the sort routine designate which column to sort on and in which direction to sort (ascending or descending)

    Each of these "wants" gave me problems, but with the assistance of a couple of you experts, I was able to find a solution. It's not the most elegant of solutions, but for my needs, it works. Here it is. Please respond with constructive comments. I, always, welcome new information.

    BTW ... the following routines/macros are assigned to buttons on the worksheets:
    SortDashboard_byName
    SortDashboard_byE0
    SortDashboard_byWorkType
    SortAdditionalInfo_byName
    SortAdditionalInfo_byWorkType
    SortAdditionalInfo_byRequirements

    Please Login or Register  to view this content.
    Here is the inelegent code to determine column letter from the column index number. Since I know for sure that the number of columns will definitely not go beyond column Z I chose this method.

    Please Login or Register  to view this content.
    Here is an example of calling the 'SortProjects' routine:

    Please Login or Register  to view this content.

    Thanks, again.
    Regards,
    Sue
    Last edited by slbrick; 04-05-2017 at 12:59 PM.

  19. #19
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Sort Rows by dynamically determining first and last row

    Hi Sue,

    I don't follow the logic with the call to the GetColumnLetter Function in the line

    Please Login or Register  to view this content.
    The lColumns value you pass will always be the same. i.e. the number of columns in the currentregion which will never change. If there are 26 columns in the current region then you'll always return "Z"

    Is there any reason why you can't use the same basis instruction as you use for the lColumns Variable. i.e.

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

  20. #20
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    54

    Re: How to Sort Rows by dynamically determining first and last row

    Richard:

    The logic behind the following

    Please Login or Register  to view this content.
    is:
    1. I want to find the ROW with the first cell that has value of Null (="").
    2. This will help me determine the Last Row, with data that I'm going to sort, by subtracting 1.
    3. I want to make sure that I do not detect the first cell with value ="" in a column to the right of my last column.
    4. When I search for the first cell with value - "" I want to restrict the Range to the area that contains data.
    5. I know that the data starts in Column 1, so I know the letter for that column is "A".
    6. I need to find the letter for the value in lColumns. That's why I use the function GetColumnLetter().
    7. It's all to restrict the range that the Find method is going to work on.


    I hope that explanation was clear.

    As for your suggestion for using

    Please Login or Register  to view this content.
    I tried that and it did not return the number of rows in the area. I would have thought that it would. I looked up the CurrentRegion property and you would think that it would work. But it just does not return the number of rows in the "region". The definition/explanation of the property with the added "Rows.Count" says that it should detect the rows with data in them until it hits a blank row. BUT ... it just is not returning the correct value.

    Thanks.
    Sue

+ 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. [SOLVED] Dynamically sort a pivot table
    By smls in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-09-2020, 01:58 AM
  2. sort dynamically and automatically by color
    By EYALIN in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-21-2015, 06:14 AM
  3. [SOLVED] Dynamically adding rows to a sort
    By Montyb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2013, 12:57 AM
  4. Replies: 1
    Last Post: 01-21-2013, 01:57 AM
  5. Logic for dynamically determining cell value
    By BrianKusch in forum Excel General
    Replies: 4
    Last Post: 02-28-2012, 07:22 PM
  6. Select Data Dynamically and then Sort
    By rvc81 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2010, 08:28 AM
  7. Dynamically determining when a month ends
    By Moomancow3k in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 01-11-2006, 07:00 PM

Tags for this Thread

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