coppied exact macro, first one works second one errors, why?

banana99

New Member
Joined
Mar 13, 2013
Messages
35
Hi, I copies a macro to filter/sort then copy, create worksheet, and then paste. I changed the descending to ascending in the second macro, that's it. Descending works fine, but can't figure out why second one (ascending) errors out with "Run-time error 91" "object variable or with block variable not set on "ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
SortFields.Clear" So, then I recorded the macro for ascending filter/sort and pasted that one, same error. My buddy who knows vba well took a look at it and couldn't figure it out. Help would be greatly appreciated :)
Code:
   'filter Descending
ActiveWorkbook.Sheets("Questions").Activate
With ActiveWorkbook.Sheets("Questions")
    Range("G2:G60").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Add Key:=Range("G2:G60"), SortOn:=xlSortOnValues, Order:= _
        xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Rows("3:60").Select
    Selection.Copy
  Worksheets.Add(After:=Worksheets("Slide 10")).Name = "Slide 11 "
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
    
End With

    'filter Ascending
ActiveWorkbook.Sheets("Questions").Activate
With ActiveWorkbook.Sheets("Questions")
    Range("G2:G60").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Add Key:=Range("G2:G60"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Rows("3:60").Select
    Selection.Copy
  Worksheets.Add(After:=Worksheets("Slide 11")).Name = "Slide 12"
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
end with
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
That's because in the ascending section you have remove Autofilter from your selection with this line:

Selection.AutoFilter

You don't need this line as the selection already has autofilter applied.

Rich (BB code):
'....
'code snippet
   Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    ActiveSheet.Paste
    
End With

    'filter Ascending
ActiveWorkbook.Sheets("Questions").Activate
With ActiveWorkbook.Sheets("Questions")
    Range("G2:G60").Select
    Selection.AutoFilter   '=======Get rid of this line!!!!!!!!!!!!!!    
       ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Questions").AutoFilter.Sort. _
        SortFields.Add Key:=Range("G2:G60"), SortOn:=xlSortOnValues, Order:= _
        xlAscending
'....
 
Upvote 0

Forum statistics

Threads
1,203,642
Messages
6,056,511
Members
444,871
Latest member
Vishal Gupta

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