0

I want to sort 4 columns for my VBA application but it gives me a error like this

"Application defined or object defined error"

this is my source code

Range("A3:X" & lastRow).Select Selection.Sort Key1:=Range("e3"), Order1:=xlAscending, _ Key2:=Range("D3"), Order2:=xlAscending, _ Key3:=Range("c3"), Order3:=xlAscending, _ Key4:=Range("f3"), Order4:=xlAscending, _ Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal 

this is working without the Key4 and Order4 I wonder what is wrong with my codes

4
  • The Range.Sort method only defines three keys (columns). Commented Sep 11, 2017 at 10:28
  • hmm do you have any suggestion ? for my concern sir @RonRosenfeld Commented Sep 11, 2017 at 10:30
  • Your question reads about sorting on 3 columns, so why do you need 4 keys? Commented Sep 11, 2017 at 10:34
  • Possible duplicate of VBA Excel Variable Sorting on Multiple Keys/Orders Commented Sep 11, 2017 at 10:45

1 Answer 1

4

I think this will give you what you're after:

With ActiveWorkbook.Worksheets(1).Sort ' set this to the relevant book and sheet .SortFields.Clear .SortFields.Add Key:=Range("E3"), Order:=xlAscending .SortFields.Add Key:=Range("D3"), Order:=xlAscending .SortFields.Add Key:=Range("C3"), Order:=xlAscending .SortFields.Add Key:=Range("F3"), Order:=xlAscending .SetRange Range("A3:X" & lastRow) .Apply End With 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.