We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
Contact Forum Editor

Send an email to our Forum Editor:


PLEASE NOTE: Your name is used only to let the Forum Editor know who sent the message. Both your name and email address will not be used for any other purpose.

Tech Helproom


It's free to register, to post a question or to start / join a discussion


 

Excel VB Custom Sort question


Desert Andy

Likes # 0

Hi

I have recorded the following macro

Range("C4:K10").Select ActiveWorkbook.Worksheets("GRID").Sort.SortFields.Clear ActiveWorkbook.Worksheets("GRID").Sort.SortFields.Add Key:=Range("C5:C10"), _ SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="MC", _ DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("GRID").Sort .SetRange Range("C4:K10") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With

When I try and put it into a subroutine code

ActiveWorkbook.Worksheets(SheetName).Sort.SortFields.Clear ActiveWorkbook.Worksheets(SheetName).Sort.SortFields.Add Key:=Range("C5:K10"), _ SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="MC", DataOption:=xlSortNormal With ActiveWorkbook.Worksheets(SheetName).Sort .SetRange Range("C5:K10") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With

Unfortunately i get a runtime error 10004 with the message 'The sort reference is not valid.

Can anyone help please. I have it written as a subroutine because I want to change the cutom sort field .

Many thanks

Like this post

Reply to this topic

This thread has been locked.



IDG UK Sites

Windows 10 release date, price, features. The next version of Windows will run on everything:....

IDG UK Sites

An Apple fan's perspective on the Windows 10 launch

IDG UK Sites

Sensor-laden kids' watch with 3G launches in Japan

IDG UK Sites

2014 Retina iMac release date rumours, specs and features: 27in 5K Retina iMac 'launching this mont'......