Sort a named range when deactivating a worksheet

The Dutchman

Board Regular
Joined
Apr 10, 2008
Messages
72
I used a macro to come up with this code which doesn't work. The code is...

Code:
Private Sub Worksheet_Deactivate()
 
[COLOR=red][B][U]Range("staff1").Select
[/U][/B][/COLOR]    ActiveWorkbook.Worksheets("Set Up").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Set Up").Sort.SortFields.Add Key:=Range("staff1") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Set Up").Sort
        .SetRange Range("I5:I14")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
End Sub

I get a "Select method of range class failed" error message. Do I have to list the worksheet related to the range?

Thx
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The red line should work fine if you're not on a chart sheet.

All the other range references, though, refer to the active sheet, not (necessarily) the sheet your'e trying to sort.

Edit: Now that you've changed changed your post ... staff1 has to be on the active sheet to be selected.

See Peter's code at http://www.mrexcel.com/forum/showpost.php?p=2559076&postcount=5
 
Last edited:
Upvote 0
This is one reason that it is desirable to avoid Selecting. Since you are leaving the sheet, you can't select a range on that sheet.

Code:
    With ActiveWorkbook.Worksheets("Set Up")
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("I5:I14"), SortOn:=xlSortOnValues _
            , Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("I5:I14")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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