Sort to Apply to worksheet

The Animal

Active Member
Joined
May 26, 2011
Messages
449
Hi.
I have recorded a sort for WE 22-01-16 worksheet and assigned to a button as below.
I have added a button to a number of additional worksheets on the same workbook that have a different names and have created buttons and assigned that macro to those buttons.
When I run the macro on those other worksheets the actual sort runs on the WE 22-01-16 worksheet rather than the actual one I have selected. I understand why its happening just don't know how to fix it.
How do I change the sort macro so it applies to only the new worksheet that I have copied it to rather than the one where I created it?
Any help would be great.

Sub SortWithBlanks()
' SortWithBlanks Macro
Range("A5:CH22").Select
ActiveWorkbook.Worksheets("WE 22-01-16").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("WE 22-01-16").Sort.SortFields.Add Key:=Range( _
"B5:B22"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"A-Z,Blanks", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("WE 22-01-16").Sort
.SetRange Range("A5:CH22")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.ScrollColumn = 70
ActiveWindow.ScrollColumn = 69
ActiveWindow.ScrollColumn = 68
ActiveWindow.ScrollColumn = 67
ActiveWindow.ScrollColumn = 65
ActiveWindow.ScrollColumn = 60
ActiveWindow.ScrollColumn = 53
ActiveWindow.ScrollColumn = 47
ActiveWindow.ScrollColumn = 39
ActiveWindow.ScrollColumn = 32
ActiveWindow.ScrollColumn = 22
ActiveWindow.ScrollColumn = 19
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 14
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 1
Range("A30:CH35").Select
ActiveWorkbook.Worksheets("WE 22-01-16").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("WE 22-01-16").Sort.SortFields.Add Key:=Range( _
"B30:B35"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"A-Z,Blanks", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("WE 22-01-16").Sort
.SetRange Range("A30:CH35")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

HackSlash

Active Member
Joined
Nov 18, 2016
Messages
360
Sure thing! I replaced every absolute declaration "Worksheets("WE 22-01-16")" with a relative declaration "ActiveSheet". You don't always know what sheet you are on but using buttons is a great way to control that! You're on your way.

Try this:
Code:
Sub SortWithBlanks()
' SortWithBlanks Macro
Range("A5:CH22").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("B5:B22"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="A-Z,Blanks", DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
    .SetRange Range("A5:CH22")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Range("A30:CH35").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("B30:B35"), _
SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="A-Z,Blanks", DataOption:=xlSortNormal

With ActiveWorkbook.ActiveSheet.Sort
    .SetRange Range("A30:CH35")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,095
Messages
5,835,363
Members
430,352
Latest member
xvidzoro

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
Top