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
Bookmarks