VBA Auto Filter Error

JPeppers

New Member
Joined
Mar 13, 2017
Messages
1
I've written code to compare the data included in 2 huge Excel files and while running the comparison code I encounter a "Run time error '91' - Object Variable or With block variable not set". Anyone know what my error is? The debug button leads me to the code bolded and underlined below:

'Copy/paste values for new count column and copy format of last column to new count column
Columns("AG:AG").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("AF:AF").Select
Application.CutCopyMode = False
Selection.Copy
Columns("AG:AG").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Add Key:=Range("AG1:AG" & lngCount_total), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("AG1").Select
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Something of a shot in the dark here but I've dealt with AutoFilter before (it's finicky), so I'll post.

I found a similar error posted online here. There may be more modification necessary but if you change the bolded line to
Code:
ActiveWorkbook.Worksheets("Old Data").Sort.SortFields.Clear
will it execute?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

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