+ Reply to Thread
Results 1 to 7 of 7

Need VBA Code to automatically sort on a numerical column highest to lowest

Hybrid View

jcicero57 Need VBA Code to... 02-04-2024, 12:50 PM
deadlyduck Re: Need VBA Code to... 02-04-2024, 01:09 PM
jcicero57 Re: Need VBA Code to... 02-04-2024, 01:24 PM
deadlyduck Re: Need VBA Code to... 02-04-2024, 04:11 PM
FlameRetired Re: Need VBA Code to... 02-04-2024, 05:55 PM
jcicero57 Re: Need VBA Code to... 02-04-2024, 08:12 PM
FlameRetired Re: Need VBA Code to... 02-05-2024, 02:17 PM
  1. #1
    Registered User
    Join Date
    06-20-2012
    Location
    Clearwater, Florida
    MS-Off Ver
    Excel 2007
    Posts
    28

    Need VBA Code to automatically sort on a numerical column highest to lowest

    I have an Excel Sheet with columns A through J. There are 1900 rows. The Numerical data to drive the sorting is in Column J2-J1900. There are formulas in Col K-O. The cells are protected, I do NOT want the rows in K-O to sort based on Column J . I DO want the rows A2 - I1900 to sort based on Col J sort

    There is a header in row A
    The sheet is password protected

    I need a VBA code to sort rows based on Col J highest to lowest numerical value without impacting the sort of Col K-O

    below is the code provided but I can't seem to get it to work

    Sub SortData()
    Dim ws As Worksheet
    Dim lR As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1") ' your sheet

    ws.Unprotect

    lR = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row

    With ws.Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("J2:J" & lastRow), SortOn:=xlSortOnValues, _
    Order:=xlDescending, DataOption:=xlSortNormal
    .SetRange Range("A1:O" & lastRow) ' assuming A as start of columns
    .Header = xlYes ' assuming you have headers
    .Apply
    End With

    ws.Protect
    End Sub


    Any help, suggestions or solutions will be appreciated. /

    Note this code was also posted on https://www.mrexcel.com/board/thread.../#post-6151933
    Last edited by jcicero57; 02-04-2024 at 08:09 PM. Reason: Notifying Cross posting

  2. #2
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    540

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    The line
     SetRange Range("A1:O" & lastRow)
    includes K to O. Is this supposed to be the case?
    Excel 365 user. To unblock a downloaded macro-enabled workbook, go to your "Downloads" folder > right click on the workbook name > click 'Properties' > check the 'Unblock' checkbox. You can now open the workbook.

  3. #3
    Registered User
    Join Date
    06-20-2012
    Location
    Clearwater, Florida
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    Thank you for the quick response I do not want to have rows K- O to sort . I only want the sort to apply to A1-J1900
    Thank you.

  4. #4
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    540

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    OK, so wouldn't you need to change that line of code to read
    .SetRange Range("A1:J" & lastRow)
    instead?

  5. #5
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2509
    Posts
    13,839

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    Administrative Note:

    jcicero57
    Welcome to the forum.

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so. Cross-posts are allowed but you must provide a link to your posts on other sites.

    Please see Forum Rule #7 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    (Note: this requirement is not optional. No help to be offered until you provide a link or, for members with fewer than 10 posts, a comment telling us where else you have posted this query.)
    Dave

  6. #6
    Registered User
    Join Date
    06-20-2012
    Location
    Clearwater, Florida
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    Thank you for the Direction
    I edited the post and linked the post with a note that it is also posted at https://www.mrexcel.com/board/thread.../#post-6151933

    Will this restore my ability to post?

    Thank you

  7. #7
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2509
    Posts
    13,839

    Re: Need VBA Code to automatically sort on a numerical column highest to lowest

    Yes. And thank you.

+ 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] Sort lowest to highest in rows
    By RobinPrice in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-25-2016, 12:36 AM
  2. How to sort a single column from lowest to highest by using formular
    By issa.o in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-12-2016, 08:41 AM
  3. [SOLVED] Cant sort % highest to lowest?
    By mikey42979 in forum Excel General
    Replies: 6
    Last Post: 12-10-2015, 02:39 PM
  4. Replies: 5
    Last Post: 06-12-2014, 12:08 AM
  5. auto sort highest to lowest
    By B1G0 in forum Excel General
    Replies: 3
    Last Post: 04-02-2014, 05:37 PM
  6. How to sort from highest to lowest
    By dneunabe in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 11-22-2013, 04:49 PM
  7. Sort from lowest to highest
    By Steved in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-10-2005, 08:07 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