+ Reply to Thread
Results 1 to 9 of 9

Last column sorting for fixed rows through VBA

Hybrid View

tabkaz Last column sorting for fixed... 10-20-2021, 06:15 AM
BadlySpelledBuoy Re: Last column sorting for... 10-20-2021, 07:45 AM
tabkaz Re: Last column sorting for... 10-20-2021, 07:48 AM
BadlySpelledBuoy Re: Last column sorting for... 10-20-2021, 08:05 AM
tabkaz Re: Last column sorting for... 10-21-2021, 01:42 AM
BadlySpelledBuoy Re: Last column sorting for... 10-21-2021, 02:10 AM
tabkaz Re: Last column sorting for... 10-21-2021, 02:12 AM
tabkaz Re: Last column sorting for... 10-21-2021, 02:32 AM
BadlySpelledBuoy Re: Last column sorting for... 10-21-2021, 02:27 AM
  1. #1
    Registered User
    Join Date
    02-27-2012
    Location
    UK
    MS-Off Ver
    Excel 365
    Posts
    65

    Last column sorting for fixed rows through VBA

    trying to fix the code for sorting on the last column of every sheet the sorting on rows to be done at Rows 3:20 & 22:32.
    Found the below code but I am unable to incorporate my desired rows in it. unable to crack how to define the rows in the below code.

    image.jpg

    Sub jusho()    
        Dim lColumn As Long
        lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        
        Range(Cells(2, 1), Cells(LastRow, lColumn)).Sort key1:=Range(Cells(2, lColumn), Cells(LastRow, lColumn)), _
           order1:=xlAscending, Header:=xlNo
    End Sub
    Last edited by tabkaz; 10-20-2021 at 06:49 AM. Reason: sample data

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,952

    Re: Last column sorting for fixed rows through VBA

    Will the rows that need sorting always be the same across all sheets?
    Will the last column be the same on each sheet?

    BSB

  3. #3
    Registered User
    Join Date
    02-27-2012
    Location
    UK
    MS-Off Ver
    Excel 365
    Posts
    65

    Re: Last column sorting for fixed rows through VBA

    Rows will be the same but the last column will change as per the data.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,952

    Re: Last column sorting for fixed rows through VBA

    Without a sample workbook it's difficult to test but give this a try.
    Sub SortMultipleSheets()
        Dim ws As Worksheet
        Dim lCol As Long
        
        For Each ws In ThisWorkbook.Sheets
            With ws
                lCol = .Cells(2, Columns.Count).End(xlToLeft).Column
                .Range("A3").Resize(18, lCol).Sort Key1:=.Cells(3, lCol)
                .Range("A23").Resize(10, lCol).Sort Key1:=.Cells(23, lCol)
            End With
        Next ws
    End Sub
    BSB

  5. #5
    Registered User
    Join Date
    02-27-2012
    Location
    UK
    MS-Off Ver
    Excel 365
    Posts
    65

    Re: Last column sorting for fixed rows through VBA

    Thanks, it works,
    it's working fine for sorting in ascending order, what to add if I have sorted in descending order for all the sheets?
    Last edited by tabkaz; 10-21-2021 at 02:11 AM.

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,952

    Re: Last column sorting for fixed rows through VBA

    Happy to help

    BSB

  7. #7
    Registered User
    Join Date
    02-27-2012
    Location
    UK
    MS-Off Ver
    Excel 365
    Posts
    65

    Re: Last column sorting for fixed rows through VBA

    it's working fine for sorting in ascending order, what to add if I have to sort in descending order for all the sheets?

  8. #8
    Registered User
    Join Date
    02-27-2012
    Location
    UK
    MS-Off Ver
    Excel 365
    Posts
    65

    Re: Last column sorting for fixed rows through VBA

    have adjusted the code for sorting in descending order,

    Sub SortMultipleSheets()
        Dim ws As Worksheet
        Dim lCol As Long
        
        For Each ws In ThisWorkbook.Sheets
            With ws
                lCol = .Cells(2, Columns.Count).End(xlToLeft).Column
                .Range("A3").Resize(18, lCol).Sort Key1:=.Cells(3, lCol), Order1:=xlDescending, Header:=xlNo, _
         MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
                        
                .Range("A23").Resize(10, lCol).Sort Key1:=.Cells(23, lCol), Order1:=xlDescending, Header:=xlNo, _
         MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
                          
    
            End With
        Next ws
    End Sub

  9. #9
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,952

    Re: Last column sorting for fixed rows through VBA

    Try this:
    Sub SortMultipleSheets()
        Dim ws As Worksheet
        Dim lCol As Long
        
        For Each ws In ThisWorkbook.Sheets
            With ws
                lCol = .Cells(2, Columns.Count).End(xlToLeft).Column
                .Range("A3").Resize(18, lCol).Sort Key1:=.Cells(3, lCol), Order1:=xlDescending
                .Range("A23").Resize(10, lCol).Sort Key1:=.Cells(23, lCol), Order1:=xlDescending
            End With
        Next ws
    End Sub
    BSB
    Last edited by BadlySpelledBuoy; 10-21-2021 at 02:29 AM.

+ 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] Add rows in Alternate columns, divide by fixed rate per column.
    By jomili in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-12-2017, 06:46 PM
  2. Replies: 0
    Last Post: 04-14-2016, 12:44 AM
  3. [SOLVED] Sorting columns with rows with a fixed "Sort" button
    By dmed99 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-07-2015, 07:05 PM
  4. Replies: 13
    Last Post: 10-13-2015, 04:17 AM
  5. Sorting 5 Columns keeping the earlier column data fixed
    By gaurav225 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-04-2014, 07:06 AM
  6. Sorting Rows by Value in a column
    By The Monk in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-26-2013, 06:37 AM
  7. Replies: 1
    Last Post: 06-02-2010, 10:56 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