sorting the autofiltered range

tore_putki1

New Member
Joined
Mar 23, 2010
Messages
6
Hi everyone,

I would like to sort the autofiltered range without copying to a sheet using multiple criteria in ascending or descending order. I have done the autofiltering OK. The autofilter range can have 0 to n number of rows in it. When I try sorting on the filtered range I get various errors and one of them being:

Run-time error '1004':
The sort reference is not valid. Make sure it's within the data you want to sort, and the first Sort By box isn't same or blank.


My code segment is the following:

With Sheets(j)
.AutoFilterMode = False
'******************************************************
'range of columns in the target sheet e.g., A2 to P2
' A1 to P1 has column heading
'I hope all the sheets have same number of columns
'******************************************************
'Column#9 = Opportunity Value, column#10 = Gross Revenue
With .Range("A1:P1")

.AutoFilter

.AutoFilter Field:=9, Criteria1:=">=10"
.AutoFilter Field:=10, Criteria1:=">=3000000"

End With
End With


With ActiveSheet.AutoFilter.Range

'*****reset these ranges filled previously in the last iteration*****
Set rngSAward = Nothing

On Error Resume Next
Set rngSAward = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With

emptyMsg = "No data with OV>=10 and GRev>=3,000,000 in: " & Sheets(j).Name
If rngSAward Is Nothing Then
MsgBox emptyMsg
Else: MsgBox "There is data with OV>=10 and GRev>=3,000,000 in: " & Sheets(j).Name

'i = rngSAward.SpecialCells(xlCellTypeVisible).Count




With rngSAward
.Sort Key1:=Range("N:N"), Order1:=xlDescending

End With

End If

'Sheets(2).Range(Cells(1, 2), Cells(12, 2)).Sort Key1:=Sheet(2).Cells(1, 2)




Any help would be greatly appreciated.
 

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.
Thank you for your reply. I have tried your suggestion. I still receive the following error:

Run-time error: 1004

reference is not valid
 
Upvote 0
I see now that your range excludes the header row, so try:

Code:
.Sort Key1:=.Range("N2"), Order1:=xlDescending, Header:=xlGuess

If that doesn't work record a macro while doing it manually and look at the code.
 
Upvote 0
How about?

Code:
.Sort Key1:=Range("N2"), Order1:=xlDescending, Header:=xlGuess

What does the macro recorder give you?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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