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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,220
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,527
Messages
5,854,270
Members
431,631
Latest member
Roneckbc

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