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

Sony Xperia Z3+ release date, price and specs: The Xperia Z4 for the UK

IDG UK Sites

Why Intel’s vision of the future is a future I want to live in

IDG UK Sites

10 amazing, creative uses of tech – and the brands behind them

IDG UK Sites

Jony Ive 'semi-retired' into new role: kicked upstairs as Chief Design Officer