help with refining a recorded macro

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I've recorded a macro that sorts about 19 tables (used to create charts).


I was wondering if there is a more efficient way to write this code? And if someone could have a crack at it?


Code:
Sub SortZoneCharts()
'
' SortZoneCharts Macro
'

'
    Sheets("Chart_Tables").Select
    Range("F287:G301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "G288:G301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("F287:G301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("H287:I301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "I288:I301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("H287:I301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("J287:K301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "K288:K301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("J287:K301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("L287:M301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "M288:M301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("L287:M301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("N287:O301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "O288:O301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("N287:O301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("P287:Q301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "Q288:Q301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("P287:Q301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
   
    Range("R287:S301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "S288:S301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("R287:S301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "U288:U301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("T287:U301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWindow.SmallScroll ToRight:=7
    Range("V287:W301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "W288:W301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("V287:W301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "Y288:Y301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("X287:Y301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("Z287:AA301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AA288:AA301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("Z287:AA301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AB287:AC301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AC288:AC301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AB287:AC301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AD287:AE301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AE288:AE301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AD287:AE301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
    Range("AF287:AG301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AG288:AG301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AF287:AG301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AH287:AI301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AI288:AI301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AH287:AI301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AJ287:AK301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AK288:AK301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AJ287:AK301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AL287:AM301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AM288:AM301"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AL287:AM301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
  
    Range("AN287:AO301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AO288:AO301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AN287:AO301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("AP287:AQ301").Select
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Chart_Tables").Sort.SortFields.Add Key:=Range( _
        "AQ288:AQ301"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Chart_Tables").Sort
        .SetRange Range("AP287:AQ301")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("AQ304").Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hope this helps.

Code:
Sub SortZoneCharts()
' SortZoneCharts Macro

Dim wsCT As Worksheet
Dim rng As Range
Dim i  As Long
Set wsCT = Sheets("Chart_Tables")
    With wsCT
        For i = 6 To 42 Step 2
            Set rng = .Range(.cells(287, i), .cells(301, i + 1))
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=wsCT.cells(288, i + 1), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                .SetRange rng
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        Next
    End With
End Sub
 
Upvote 0
Worked like a charm. Thanks for the help. Very much appreciated.

Hope this helps.

Code:
Sub SortZoneCharts()
' SortZoneCharts Macro

Dim wsCT As Worksheet
Dim rng As Range
Dim i  As Long
Set wsCT = Sheets("Chart_Tables")
    With wsCT
        For i = 6 To 42 Step 2
            Set rng = .Range(.cells(287, i), .cells(301, i + 1))
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=wsCT.cells(288, i + 1), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                .SetRange rng
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top