Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to sort my chart from a-z I am having an issue finding a way for this to affect all the columns in the chart.

What I have tried:

LastRow = Cells(Rows.Count, 17).End(xlUp).Row
LastCol = Cells(7, Columns.Count).End(xlToLeft).Column

    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", LastRow), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("Q7", LastCol)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

This is code from a recorded macro that I tried to edit but I am getting an error saying range of object global failed.
Posted
Updated 24-Feb-22 5:51am
v2

1 solution

ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
   ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", Cells(LastRow, "Q")), _
       SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
   With ActiveWorkbook.Worksheets("Data").Sort
       .SetRange Range("Q7", Cells(LastRow, LastCol))
       .Header = xlNo
       .MatchCase = False
       .Orientation = xlTopToBottom
       .SortMethod = xlPinYin
       .Apply
   End With
 
Share this answer
 
Comments
Maciej Los 24-Feb-22 14:56pm    
Is this an answer or further part of your question?
Human Person 24-Feb-22 15:01pm    
answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900