Adapt variable column macro for other worksheets

JONPM

New Member
Joined
Apr 17, 2013
Messages
24
I have this Macro for alphabetically sorting a cells in column AB

I would like the macro to work with other worksheets however the other worksheets in the workbook have different titles to BRIM.

What would I change in this macro so that it would work with other worksheets??

Many thanks in advance


' PWRII Macro''LR = Worksheets("BRIM").Range("AB" & Rows.Count).End(xlUp).RowActiveWorkbook.Worksheets("BRIM").Sort.SortFields.ClearActiveWorkbook.Worksheets("BRIM").Sort.SortFields.Add Key:=Range("AB2:AB" & LR) _, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormalWith ActiveWorkbook.Worksheets("BRIM").Sort .SetRange Range("A1:AB" & LR) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .ApplyEnd WithEnd Sub</pre>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Everywhere you see "ActiveWorkbook.Worksheets("BRIM")" in the code, just replace it With "ActiveSheet" (without the quotemarks). Then make sure the sheet you want to run the code on is the activesheet by selecting it and then run the macro.
 

ZAX

Well-known Member
Joined
Jul 5, 2012
Messages
715
Everywhere you see "ActiveWorkbook.Worksheets("BRIM")" in the code, just replace it With "ActiveSheet" (without the quotemarks). Then make sure the sheet you want to run the code on is the activesheet by selecting it and then run the macro.

Here's An Easier Way To do This Because It Will Apply It To All Sheets Automatically:
Code:
Sub PWRII()
For SCount = 1 To Sheets.Count
    LR = Worksheets(SCount).Range("AB" & Rows.Count).End(xlUp).Row
    ActiveWorkbook.Worksheets(SCount).Sort.SortFields.ClearActiveWorkbook.Worksheets(SCount).Sort.SortFields.Add Key:=Range("AB2:AB" & LR) _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets(SCount).Sort
        .SetRange Range("A1:AB" & LR)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Next
End Sub
ZAX
 

JONPM

New Member
Joined
Apr 17, 2013
Messages
24
Thanks I used the ActiveSheet method — great instructions it's meaning that through trial, error and simply doing I'm beginning to understand how macros work and how I can write them.

Thanks J
 
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,163,864
Messages
5,834,057
Members
430,260
Latest member
MANICX100

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