Results 1 to 20 of 25

Thread: Data Using Double Click on Header but multiple range

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    OK, try this

    There's no real error checking to fail softly (like mis-spelling one of the categories), but checks can be added

    Option Explicit
    
    Sub SortBlocks()
        Dim rCategory As Range        
        Application.ScreenUpdating = False    
        With ActiveSheet
            Set rCategory = .Columns(2)
            Call SortBlock(rCategory, "Auto Ancillaries")
            Call SortBlock(rCategory, "Auto")
            Call SortBlock(rCategory, "Banks")
        End With
        Application.ScreenUpdating = True
        MsgBox "Sorted"
    End Sub
    
    Private Sub SortBlock(r As Range, s As String)
        Dim rLastColumn As Range, rSort As Range, rStart As Range, rEnd As Range    
        With r
            Set rStart = .Find(What:=s, After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlWhole).Offset(1, 0)
            Set rEnd = rStart.End(xlDown)
            Set rLastColumn = .Parent.Cells(6, .Parent.Columns.Count).End(xlToLeft).EntireColumn
            Set rSort = Range(rStart, Intersect(rEnd.EntireRow, rLastColumn))
        End With
        With r.Parent.Sort
            .SortFields.Clear
            .SortFields.Add Key:=rSort.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange rSort
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    
    Attached Files Attached Files
    Last edited by Aussiebear; 03-16-2025 at 12:52 AM.
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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