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

How to use an Apple Watch: Everything you need to know about the Apple Watch

IDG UK Sites

Why Scottish Tablet is better than the iPad mini

IDG UK Sites

How Microsoft's HoloLens AR headset will work without needing a computer or phone

IDG UK Sites

Apple MacBook 1.1 GHz review (Retina, 12-inch, Early 2015): The future of Apple laptops