VBA Sorting "ActiveSheet or Worksheet"

JimTobin2

New Member
Joined
Jul 4, 2014
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have a sort macro that I've recorded and want to use it on various worksheets (one at a time upon activation) with in a workbook. The sort macro has the worksheet name of the worksheet at the time of the recording and so I'm not able to use it for other worksheets in the workbook (e.g. Catalina). I would like to make the macro flexible so the sort will work on any active worksheet I select in the workbook.

I'm relatively new at this and would appreciate a bit of direction.

Thanks in advance.

JJT

The recoded code
VBA Code:
'
    ActiveCell.Range("A1:J151").Select
    ActiveWorkbook.Worksheets("Catalina").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Catalina").AutoFilter.Sort.SortFields.Add2 Key:= _
        ActiveCell.Range("A1:A150"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Catalina").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Well I guess the answer is ActiveSheet, unless I am missing something here. Try substituting ActiveWorkbook.ActiveSheet. for ActiveWorkbook.Worksheets("Catalina"). Let us know if that helps. Also if you need help in the Range let us know.
 
Upvote 0

Forum statistics

Threads
1,215,452
Messages
6,124,915
Members
449,195
Latest member
Stevenciu

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