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>
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,564
Messages
5,596,870
Members
414,106
Latest member
Tigretto

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