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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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