Results 1 to 20 of 39

Thread: AutoFilter Percent Values +-5%

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #24
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,894
    Location
    FWIW, it seems to work OK here



    Capture.JPG


    Book1.xlsm is 'ThisWorkbook' with the macros and data. It's possible that using ActiveWorkbook didn't return the workbook that you thought it was

    Book2.xlsx is the one with "F132:F146" values


    Option Explicit
    
    
    Sub StatTest()
        Dim wb1         As Workbook, wb2 As Workbook
        Dim ws1         As Worksheet, ws2 As Worksheet
        Dim rng         As Range, rng9   As Range
        Dim lr          As Long
        Dim MaxValue    As Double
        Dim MinValue    As Double
        
        
        Application.ScreenUpdating = False
        
        Set wb1 = ThisWorkbook
        Set ws1 = wb1.Sheets("Sheet1")
        
        Set wb2 = Workbooks("Book2.xlsx")
        Set ws2 = wb2.Sheets("Sheet1")
        
        lr = ws1.Cells(Rows.Count, "A").End(xlUp).Row
        
        Set rng = ws1.Range("A2:J" & lr)            'Made used range smaller for easier overview. Still same problem.
        Set rng9 = ws2.Range("F132:F146")
        
        rng9.NumberFormat = "0%"                    'To show and make sure rng9 and Col.F have same format.
        ws1.Range("F2:F" & lr).NumberFormat = "0%"  'To show and make sure rng9 and Col.F have same format.
        
        
        MaxValue = Application.WorksheetFunction.Max(rng9)
        MaxValue = MaxValue + 0.05
        MinValue = MaxValue - 0.1
        
        With ws1
            .AutoFilterMode = False
            .Range("A1:J1").AutoFilter
            rng.AutoFilter Field:=6, Criteria1:=">=" & MinValue, Operator:=xlAnd, Criteria2:="<=" & MaxValue    'Works for NumberFormat= numbers, not NumberFormat= %
        End With
        
        Application.ScreenUpdating = True
    End Sub


    Matter of personal style, but I'd rearrange the macro to group the workbook processes together

    Option Explicit
    
    
    Sub StatTest()
        Dim wb1         As Workbook, wb2 As Workbook
        Dim ws1         As Worksheet, ws2 As Worksheet
        Dim rng         As Range, rng9   As Range
        Dim lr          As Long
        Dim MaxValue    As Double
        Dim MinValue    As Double
        
        
        Application.ScreenUpdating = False
        
        Set wb1 = ThisWorkbook
        Set ws1 = wb1.Sheets("Sheet1")
        lr = ws1.Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = ws1.Range("A2:J" & lr)            'Made used range smaller for easier overview. Still same problem.
        ws1.Range("F2:F" & lr).NumberFormat = "0%"  'To show and make sure rng9 and Col.F have same format.
        
        
        Set wb2 = Workbooks("Book2.xlsx")
        Set ws2 = wb2.Sheets("Sheet1")
        Set rng9 = ws2.Range("F132:F146")
        rng9.NumberFormat = "0%"                    'To show and make sure rng9 and Col.F have same format.
        
        
        MaxValue = Application.WorksheetFunction.Max(rng9)
        MaxValue = MaxValue + 0.05
        MinValue = MaxValue - 0.1
        
        With ws1
            .AutoFilterMode = False
            .Range("A1:J1").AutoFilter
            rng.AutoFilter Field:=6, Criteria1:=">=" & MinValue, Operator:=xlAnd, Criteria2:="<=" & MaxValue    'Works for NumberFormat= numbers, not NumberFormat= %
        End With
        
        Application.ScreenUpdating = True
    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

Tags for this Thread

Posting Permissions

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