Object Variable Error

DarrenF

Board Regular
Joined
Jun 9, 2014
Messages
90
I'm trying the run the below VBA and I'm getting the below error when doing so on the below line:

ActiveWorkbook.Worksheets("Assigned Today - SD").AutoFilter.Sort.SortFields. _
Clear

Run-time error '91':
Object variable or With block variable not set

My worksheet name that I'm running the VBA on is "Assigned Today - SD", not sure how to fix the error.



Sub find_data_SD()


Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim selectdate As String
Dim selectname As String
Dim finalrow As Integer
Dim i As Integer


Set datasheet = Sheet19
Set reportsheet = Sheet18
selectdate = reportsheet.Range("B1").Value
selectname = reportsheet.Range("C1").Value


reportsheet.Range("B13:H200").ClearContents


datasheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To finalrow
If Cells(i, 15) = selectdate And Cells(i, 30) = selectname Then
Range(Cells(i, 31), Cells(i, 37)).Copy
reportsheet.Select
Range("B2000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
datasheet.Select
End If

Next i


ActiveWorkbook.Worksheets("Assigned Today - SD").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Assigned Today - SD").AutoFilter.Sort.SortFields. _
Add Key:=Range("G:G"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Assigned Today - SD").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With




reportsheet.Select
Range("B1").Select


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You will get that error message if you do not actually have any filters currently on that sheet.
 
Upvote 0
You are welcome.

If the issue is that sometimes there may be filters, and other times not, what people often do is simply tell Excel to ignore that error, i.e.
Code:
On Error Resume Next
ActiveWorkbook.Worksheets([COLOR=#333333]"Assigned Today - SD"[/COLOR]).AutoFilter.Sort.SortFields. _
Clear
On Error GoTo 0
So then it will just continue on to the next step without posting any errors to the screen.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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