Problem with list sorting

MrOzman

New Member
Joined
May 16, 2007
Messages
49
Hi

I have this code to sort a list

Sub Alphabetise()
'
c = Sheets("Data").Range("L2").Value + 1
Sheets("Clients").Range("A3:Z" & c).Sort Key1:=Range("F3"), Order1:=xlAscending, Key2:=Range("J3") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
End Sub

My problem is that it will only work if the sheet, Clients, is the active worksheet. If any other worksheet is active when it runs I get an error. Can anybody tell me why? And, more importantly, can anyone tell me how to make it work in the background or make the Clients worksheet active without making it visible?

Thanks
 

Some videos you may like

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,970
The Sort Keys (ranges) are unqualified and on the active sheet.
The Range is on sheet Clients.
Hence the error. Try

Code:
c = Sheets("Data").Range("L2").Value + 1

With Sheets("Clients")
    .Range("A3:Z" & c).Sort Key1:=[COLOR="red"][U].[/U][/COLOR]Range("F3"), Order1:=xlAscending, _
        Key2:=[COLOR="Red"][U].[/U][/COLOR]Range("J3"), Order2:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
End Wtih
 

Watch MrExcel Video

Forum statistics

Threads
1,127,665
Messages
5,626,173
Members
416,166
Latest member
Archimed

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
Top