Results 1 to 3 of 3

Autofilter Macro Failing because object variable not set

Threaded View

dcgrove Autofilter Macro Failing... 06-18-2009, 11:55 AM
Andy Pope Re: Autofilter Macro Failing... 06-19-2009, 04:37 AM
sukumar1 Re: Autofilter Macro Failing... 12-30-2016, 02:54 AM
  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Autofilter Macro Failing because object variable not set

    Hello, I am trying to sort and unsort some data using a toggle button. the code for the toggle button calls two macros, AFauditasc and AFAudits. The code for all three is below. the whole operation is failing on the red highlighted lines depending on which macro is being called. If I run them individually the first will work, but the second fails every time. It does not matter which macro I run first as both will fail. what am I doing wrong? The error code I am getting is Run time erro 91: Object variable or With block variable not set. Both Macros were recorded with the macro recorder.

    This macro sorts the data ascending.

    Sub AFAuditasc()
    '
    ' AFAuditasc Macro
    '
    
    '
        Range("A6:AN6").Select
        Range("AN6").Activate
        Selection.AutoFilter
        ActiveWorkbook.Worksheets("excel").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("excel").AutoFilter.Sort.SortFields.Add Key:=Range( _
            "AL6"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("excel").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    This macro sorts the data descending.

    Sub AFAudits()
    '
    ' AFAudits Macro
    '
    
    '
        Range("A6:AN6").Select
        Range("AN6").Activate
        Selection.AutoFilter
        ActiveWorkbook.Worksheets("excel").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("excel").AutoFilter.Sort.SortFields.Add Key:=Range( _
            "AL6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("excel").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    This is the code for the toggle button. It seems to work fine.

    Private Sub ToggleButton2_Click()
        If ToggleButton2.Value = False Then
        Module2.AFAudits
            ToggleButton2.Caption = "Sort Total Audits Ascending"
        Else
        Module2.AFAuditasc
            ToggleButton2.Caption = "Sort Total Audits Descending"
    End If
    End Sub
    Last edited by dcgrove; 06-18-2009 at 12:33 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1