Results 1 to 9 of 9

Thread: auto filter on adding rows

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    I'd do something like this


    In standard module

    Option Explicit
    
    Sub SortData(ws As Worksheet)
    
        Dim r As Range, r1 As Range
        
        Application.ScreenUpdating = False
            
        With ws
            
            Set r = .Range("A6").CurrentRegion
            Set r1 = r.Cells(2, 1).Resize(r.Rows.Count - 1, r.Columns.Count)
        
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=r1.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SortFields.Add Key:=r1.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SortFields.Add Key:=r1.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SortFields.Add Key:=r1.Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SetRange r
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        
            Set r = .Range("A6").CurrentRegion
        End With
            
        Set r1 = r.Cells(2, 1).Resize(r.Rows.Count - 1, r.Columns.Count)
            
        With r1
            .HorizontalAlignment = xlCenter
            .Columns(3).NumberFormat = "m/d/yyyy h:mm"
            .Columns(4).NumberFormat = "m/d/yyyy h:mm"
            .Columns(5).Formula = "=IF(OR(C7=0,D7=0),"""",D7-C7)"
            .Columns(5).NumberFormat = "h:mm"
        End With
        Application.ScreenUpdating = False
    
    '    MsgBox "Sorted"
    
    End Sub



    In Sheet1 code module

    Private Sub Worksheet_Activate()
        Call SortData(Me)
    End Sub
    
    Private Sub Worksheet_Deactivate()
        Call SortData(Me)
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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
  •