Rewrite Code as a Sub to work on Active Sheet

user29383902

New Member
Joined
Dec 6, 2024
Messages
7
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi, Please I have this function, which is part of a a Sub macro in VBA, it replaces my old way of filtering, I want to use it as a standalone code for filtering in excel in any active sheet. By just pressing a shortcut like ctrl + w and it works on that sheet.





VBA Code:
Function FilterX(ws As Worksheet) As Long

    Dim rng As Range, dict, c
    Dim lastrow As Long, n As Long
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' configure filter column, tolerance
    With dict
    '   .Add "L"
        .Add "M", 0 ' +/- 0
    '   .Add "N",
        .Add "O", 1 ' +/- 1
    '   .Add "P",
    '   .Add "Q",
        .Add "R", 2
    '   .Add "S",
    '   .Add "T",
        .Add "U", 1
    '   .Add "V",
        .Add "W", 1
    '   .Add "X",
    '   .Add "Y",
        .Add "Z", 2
    End With

    With ws
        ' remove filter
        If .FilterMode = True Then .ShowAllData
       
        ' apply fliter
        lastrow = .UsedRange.Row + .UsedRange.Rows.Count - 1
        If lastrow < 3 Then
             FilterX = 0
             Exit Function
        End If
        
        Set rng = .Range("A1:AZ" & lastrow)
        'Debug.Print ws.Name, rng.Address
        
        ' apply filter to columns M, O, R, U, W, Z
        For Each c In dict.keys
            n = Cells(1, c).Column ' column number
            ' dict(c) is tolerance +/- on rows 2 value
            rng.AutoFilter Field:=n, Criteria1:=">=" & (.Cells(2, n) - dict(c)), _
                    Operator:=xlAnd, Criteria2:="<=" & (.Cells(2, c) + dict(c))
        Next
        
        ' return count
      On Error Resume Next 'skips error code when no cells are found
        FilterX = .Range("A3:A" & lastrow).SpecialCells(xlCellTypeVisible).Count
    End With
    
End Function
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi, Please I have this function, which is part of a a Sub macro in VBA, it replaces my old way of filtering, I want to use it as a standalone code for filtering in excel in any active sheet. By just pressing a shortcut like ctrl + w and it works on that sheet.





VBA Code:
Function FilterX(ws As Worksheet) As Long

    Dim rng As Range, dict, c
    Dim lastrow As Long, n As Long
    Set dict = CreateObject("Scripting.Dictionary")
   
    ' configure filter column, tolerance
    With dict
    '   .Add "L"
        .Add "M", 0 ' +/- 0
    '   .Add "N",
        .Add "O", 1 ' +/- 1
    '   .Add "P",
    '   .Add "Q",
        .Add "R", 2
    '   .Add "S",
    '   .Add "T",
        .Add "U", 1
    '   .Add "V",
        .Add "W", 1
    '   .Add "X",
    '   .Add "Y",
        .Add "Z", 2
    End With

    With ws
        ' remove filter
        If .FilterMode = True Then .ShowAllData
      
        ' apply fliter
        lastrow = .UsedRange.Row + .UsedRange.Rows.Count - 1
        If lastrow < 3 Then
             FilterX = 0
             Exit Function
        End If
       
        Set rng = .Range("A1:AZ" & lastrow)
        'Debug.Print ws.Name, rng.Address
       
        ' apply filter to columns M, O, R, U, W, Z
        For Each c In dict.keys
            n = Cells(1, c).Column ' column number
            ' dict(c) is tolerance +/- on rows 2 value
            rng.AutoFilter Field:=n, Criteria1:=">=" & (.Cells(2, n) - dict(c)), _
                    Operator:=xlAnd, Criteria2:="<=" & (.Cells(2, c) + dict(c))
        Next
       
        ' return count
      On Error Resume Next 'skips error code when no cells are found
        FilterX = .Range("A3:A" & lastrow).SpecialCells(xlCellTypeVisible).Count
    End With
   
End Function
 
Upvote 0
Here is your Funtion changed to a Subroutine. Follow instructions below.
VBA Code:
Sub FilterX(ws As Worksheet) As Long

    Dim rng As Range, dict, c
    Dim lastrow As Long, n As Long
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' configure filter column, tolerance
    With dict
    '   .Add "L"
        .Add "M", 0 ' +/- 0
    '   .Add "N",
        .Add "O", 1 ' +/- 1
    '   .Add "P",
    '   .Add "Q",
        .Add "R", 2
    '   .Add "S",
    '   .Add "T",
        .Add "U", 1
    '   .Add "V",
        .Add "W", 1
    '   .Add "X",
    '   .Add "Y",
        .Add "Z", 2
    End With

    With ws
        ' remove filter
        If .FilterMode = True Then .ShowAllData
       
        ' apply fliter
        lastrow = .UsedRange.Row + .UsedRange.Rows.Count - 1
        If lastrow < 3 Then
             FilterX = 0
             Exit Sub
        End If
        
        Set rng = .Range("A1:AZ" & lastrow)
        'Debug.Print ws.Name, rng.Address
        
        ' apply filter to columns M, O, R, U, W, Z
        For Each c In dict.keys
            n = Cells(1, c).Column ' column number
            ' dict(c) is tolerance +/- on rows 2 value
            rng.AutoFilter Field:=n, Criteria1:=">=" & (.Cells(2, n) - dict(c)), _
                    Operator:=xlAnd, Criteria2:="<=" & (.Cells(2, c) + dict(c))
        Next
        
        ' return count
      On Error Resume Next 'skips error code when no cells are found
        FilterX = .Range("A3:A" & lastrow).SpecialCells(xlCellTypeVisible).Count
    End With
    
End Sub
Click on Developer -> Click on Macros -> Find your Macro and highlight it. -> Click Options. Add your keyboard shortcut.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top