Macro adjustment

petersing089

New Member
Joined
Aug 5, 2017
Messages
2
Hello: I have read some ways to make this work but when I try them it doesn't. I want the macro below to be part of my file, then weekly I have to save the data into a new file and the tab where this data is located gets a new name. So I would like the macro to work on the active sheet, but it is looking for the old sheet name. Seems so simple but I am not a macro enabled person! See below. I want the macro to work on the active sheet regardless of workbook or sheet name.

Code:
Sub DivisionSort()
'
' DivisionSort Macro
'
' Keyboard Shortcut: Ctrl+d
'
    ActiveCell.Offset(0, 3).Range("A1:H13").Select
    Selection.Cut
    ActiveCell.Offset(0, -1).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, -2).Range("A1:J13").Select
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Add Key:=ActiveCell.Offset(0, _
        4).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("14").Sort
        .SetRange ActiveCell.Range("A1:J13")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Add Key:=ActiveCell.Offset(0, _
        3).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("14").Sort
        .SetRange ActiveCell.Range("A1:J13")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("14").Sort.SortFields.Add Key:=ActiveCell.Offset(0, _
        2).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("14").Sort
        .SetRange ActiveCell.Range("A1:J13")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Untested, but should work for the activesheet only, try:
Code:
Sub DivisionSort()
'
' DivisionSort Macro
'
' Keyboard Shortcut: Ctrl+d
'
    ActiveCell.Offset(0, 3).Range("A1:H13").Select
    Selection.Cut
    ActiveCell.Offset(0, -1).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, -2).Range("A1:J13").Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.add key:=ActiveCell.Offset(0, _
        4).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange ActiveCell.Range("A1:J13")
        .header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.add key:=ActiveCell.Offset(0, _
        3).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.ActiveSheet.Sort
        .SetRange ActiveCell.Range("A1:J13")
        .header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.add key:=ActiveCell.Offset(0, _
        2).Range("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange ActiveCell.Range("A1:J13")
        .header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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