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

bmthomps

New Member
Joined
Feb 13, 2013
Messages
9
I am getting the stated error when I run the macro. I believe it is because I need to specify the range I want the sort to be applied to. I have put the debug highlighted row in bold. How do I code it so that it applies the filter and sort to B26:AN229 ?

ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Add Key:= _
Range("E26:E229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Add Key:= _
Range("G26:G229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Add Key:= _
Range("D26:D229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Add Key:= _
Range("AC26:AC229"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort.SortFields.Add Key:= _
Range("B26:B229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("NEW - BW").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$B$25:$AN$229").AutoFilter Field:=6, Criteria1:= _
"=available - new", Operator:=xlOr, Criteria2:="=Available - Resale"
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Is autofilter turned on?

Try moving your autofilter line at the beginning:

You can also make the code easier by using a With statement to keep from repeating the same things.

Code:
ActiveWorkbook.Worksheets("NEW - BW").Range("$B$25:$AN$229").AutoFilter Field:=6, Criteria1:= _
"=available - new", Operator:=xlOr, Criteria2:="=Available - Resale"




With ActiveWorkbook.Worksheets("NEW - BW").AutoFilter
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=Range("E26:E229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("G26:G229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("D26:D229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("AC26:AC229"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("B26:B229"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
I tried that and now it gives me the error '9' Subscript out of range. The highlighted lines are the top 2
 
Upvote 0
There could be several issues.

1) Is the sheet name correct?
2) you list 2 criteria. 1 is mixed case and the other is all lower case. Do these match values in Column G?
3) Note that since the range starts in Column B, the 6th field is G. Is that the field that you want to filter?
 
Upvote 0

Forum statistics

Threads
1,215,316
Messages
6,124,228
Members
449,149
Latest member
mwdbActuary

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