Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49946

AutoFilter and Sort using VBA

$
0
0
Hi everone,

I am trying to get a small piece of my procedure to work. On the below line in red font, I receive error 438: Object doesn't support this property or method.

I am simply trying to autofilter a table, sort on the rightmost field, and then only show values > 0. The below code is a small piece of my procedure. The variable "lngLastRow" value is 1655.

Code:

With wksOutlier

    'sort by expected outlier payment and filter > $0
    .AutoFilterMode = False
    .Range("A1").AutoFilter
    With .AutoFilter.Sort.SortFields
        .Clear
        .Add Key:=.Range("A1").End(xlToRight).Resize(lngLastRow), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
    With .AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    .Range("A1").Resize(lngLastRow, .Range("A1").End(xlToRight).Column).AutoFilter _
        .Range("A1").End(xlToRight).Column, Criteria1:=">0", Operator:=xlAnd
   
End With

I tried recording a macro, and it came up with something very similar:

Code:

    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Outliers").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Outliers").AutoFilter.Sort.SortFields.Add _
        Key:=Range("BS1:BS1655"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Outliers").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Range("$A$1:$BS$1655").AutoFilter Field:=71, Criteria1:=">0", _
        Operator:=xlAnd

Am I missing something simple? Does the worksheet need to be active?

Thanks,
Jason

Viewing all articles
Browse latest Browse all 49946

Trending Articles