Consulting

Results 1 to 3 of 3

Thread: Looping through worksheets to sort

  1. #1
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    5
    Location

    Looping through worksheets to sort

    I am trying to loop through all sheets in a WB and sort based on Column R decreasing. I have tried multiple different methods from various forums and I continue to get runtime error on my ".Apply". I have attached a sample workbook with a few sheets and a little code, the number of rows changes so i need the range to be dynamic.

    Thanks for the help
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try this

    BTW your description said "decreasing" but your macro said "xlAscending"

    Option Explicit
    
    
    Sub SortSheetsColR()
        Dim ws As Worksheet
        Dim r As Range, r1 As Range
    
    
        Application.ScreenUpdating = False
    
    
        For Each ws In Sheets
            With ws
            
                Set r = .Cells(1, 1).CurrentRegion
                Set r1 = r.Cells(2, 1).Resize(r.Rows.Count - 1, r.Columns.Count)
            
                With .Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=r1.Columns(18), Order:=xlAscending
                    .SetRange r
                    .Header = xlYes
                    .Apply
                End With
            End With
            
        Next ws
            
        Application.ScreenUpdating = True
            
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    5
    Location
    Sheesh, that would do it. Thank you for the help..

    Callum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •