Need C# code for Set Auto Filter in Excel

gil.silva

Active Member
Hello,

Code:
Dim xl, wb, ws, r As Object

Try

    xl = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    wb = xl.activeworkbook
    ws = wb.activesheet
    r = ws.range(Ref)

    r.autofilter(field:=Field, Criteria1:=Criteria, VisibleDropDown:=True)
 
Catch e As Exception
    Throw e
Finally
    xl = Nothing
    wb = Nothing
    ws = Nothing
    r = Nothing
End Try

with the Inputs:
Ref - range in the format "A:A"
Field - number of the column to filter by, example: 5
Criteria - formula to filter, example: >50
 

Rupali Agarwal

New Member
Hello,

Code:
Dim xl, wb, ws, r As Object

Try

    xl = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
    wb = xl.activeworkbook
    ws = wb.activesheet
    r = ws.range(Ref)

    r.autofilter(field:=Field, Criteria1:=Criteria, VisibleDropDown:=True)

Catch e As Exception
    Throw e
Finally
    xl = Nothing
    wb = Nothing
    ws = Nothing
    r = Nothing
End Try

with the Inputs:
Ref - range in the format "A:A"
Field - number of the column to filter by, example: 5
Criteria - formula to filter, example: >50
Thanks
 
Top