AutoFilter

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,062
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code, it is giving me error as "AutoFilter method of Range class failed" i tired a lot not sure as how to fix this.

VBA Code:
Sub test()
Dim ws As Worksheet
Dim lRow As Long

Set ws = ThisWorkbook.Worksheets("Weekly RawFile")
lRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row ' Assuming data is in column A

' Apply AutoFilter
If lRow > 1 Then ' Check if there is data in the worksheet
ws.Range("A1:K" & lRow).AutoFilter Field:=5, Criteria1:=Array("<>Internet", "<>More than one service affected", "<>VOIP", "<>Jawwy TV", "<>VAS", "<>IOT Smart Service"), Operator:=xlFilterValues  'Error here
Else
    MsgBox "No data found in the worksheet.", vbExclamation
End If

End Sub


FTTH_Technial_Complaints_Weekly_ReportV2.xlsm
ABCDEFGHIJK
1WEKCREADTED_DATESR_NUMPH_NUMBERSR_AREASR_SUB_AREACOMPLAINT_TYPECOMPLAINT_SOURCEX_STC_REGIONX_STC_TECHNOLOGYCITC_REGION
21721-04-20241-1409870298883125391193InternetNo Internet connectionTechnicalNormalWESTERNFTTHMakkah_Province
31721-04-20241-1409870566655112292969More than one service affectedAll services not workingTechnicalNormalCENTRALFTTHRiyadh_Province
41828-04-20241-1410690631638172200654E-channel
51721-04-20241-1409871555445112255820InternetSlow BrowsingTechnicalNormalCENTRALFTTHRiyadh_Province
61721-04-20241-1409872368335122680312InternetNo Internet connectionTechnicalNormalWESTERNFTTHMakkah_Province
71721-04-20241-1409865775992114546441InternetNo Internet connectionTechnicalNormalCENTRALFTTHRiyadh_Province
81721-04-20241-1409871181824172201246More than one service affectedAll services not workingTechnicalNormalSOUTHERNFTTHAsir_Province
91721-04-20241-1409874594052138176995More than one service affectedAll services not workingTechnicalNormalEASTERNFTTHEastern_Province
101828-04-20241-1410690631638172200654stc play
111721-04-20241-1409876022040112785992More than one service affectedAll services not workingTechnicalNormalCENTRALFTTHRiyadh_Province
121721-04-20241-1409872675926137210312InternetOLO ONT DownTechnicalNormalEastFTTHEastern_Province
131828-04-20241-1410690631638172200654Voice
141721-04-20241-1409875454486163237349InternetNo Internet connectionTechnicalNormalCENTRALFTTHQassim_Province
151721-04-20241-1409876852597133443676InternetOLO ONT DownTechnicalNormalEastern RegionFTTHEastern_Province
161721-04-20241-1409875230400144245080InternetSpeed UpgradeTechnicalNormalWESTERNFTTHTabuk_Province
171721-04-20241-1409876116433138092703InternetOLO ONT DownTechnicalNormalEastFTTHEastern_Province
181721-04-20241-1409877411345122061064InternetNo Internet connectionTechnicalNormalWESTERNFTTHMakkah_Province
191721-04-20241-1409877772102112320320More than one service affectedLoss of signalTechnicalNormalCENTRALFTTHRiyadh_Province
201721-04-20241-1409878786957112482745More than one service affectedAll services not workingTechnicalNormalCENTRALFTTHRiyadh_Province
211828-04-20241-1410690631638172200654test
221828-04-20241-1410690631638172200654Qitaf Voucher
231828-04-20241-1410690631638172200654Service
Weekly RawFile
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
VBA Code:
Sub test_1()
    Dim ws As Worksheet
    Dim oDic As Object 'Scripting.Dictionary
    Dim i As Long
    Dim v As Variant
    Dim vArr As Variant

    'Array of excluded elements
    vArr = Split("Internet,More than one service affected,VOIP,Jawwy TV,VAS,IOT Smart Service", ",")

    Set ws = ThisWorkbook.Worksheets("Weekly RawFile")

    On Error Resume Next
    ws.ShowAllData
    On Error GoTo 0

    If Not ws.AutoFilterMode Then
        ws.Range("A1").AutoFilter
    End If

    v = ws.AutoFilter.Range.Columns(5).Value
    
    If TypeName(v) = "String" Then
        MsgBox "No data found in the worksheet.", vbExclamation
    Else
        Set oDic = CreateObject("Scripting.Dictionary")

        On Error Resume Next
        'Add unique values to the Dictionary
        For i = 2 To UBound(v)
            oDic.Add v(i, 1), 0
        Next i
        
        'Remove excluded elements from the Dictionary
        For i = 0 To UBound(vArr)
            oDic.Remove vArr(i)
        Next i
        
        'Filtered the other elements
        ws.AutoFilter.Range.AutoFilter Field:=5, Criteria1:=oDic.Keys(), Operator:=xlFilterValues
        On Error GoTo 0
    End If

End Sub
Artik
 
Upvote 0
Solution

Forum statistics

Threads
1,216,028
Messages
6,128,395
Members
449,446
Latest member
CodeCybear

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