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

Best Christmas 2014 UK tech deals, Boxing Day 2014 UK tech deals & January sales 2015 UK tech...

IDG UK Sites

Chromebooks: ready for the prime time (but not for everybody)

IDG UK Sites

Hands-on with Sony's latest smartglasses

IDG UK Sites

Apple TV expert tips: get US Apple TV content, watch Google Play, use multiple Apple IDs and more