Sorting macro by worksheet name

sdel_nevo

New Member
Joined
Jan 16, 2015
Messages
28
Hi everyone

i have created a macro that sorts a range of cells F8:F21 accending, this works really well
the code i use for this is

Range("E7:H21").Select
ActiveWorkbook.Worksheets("BNS").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("BNS").Sort.SortFields.Add Key _
:=Range("F8:F21"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("BNS").Sort
.SetRange Range("E7:H21")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

how can i change this code so that the active worksheet name is captured?
this code runs on the worksheet "BNS"

all the worksheets are dynamically created from a list and they can be different each time the spreadsheet runs,
is it possible to change this ActiveWorkbook.Worksheets("BNS").Sort.SortFields.Clear

to capture the active worksheet name?

many thanks

steve
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
For each occurrence of this: ActiveWorkbook.Worksheets("BNS") substitute Activesheet.
 
Upvote 0
Hi JoeMo

wow that was quick :)

is i just replace ActiveWorkbook.Worksheets("BNS").Sort.SortFields.Clear
with
Activesheet.Sort.SortFields.Clear

is this correct? and also for the other lines
 
Upvote 0
Hi JoeMo

wow that was quick :)

is i just replace ActiveWorkbook.Worksheets("BNS").Sort.SortFields.Clear
with
Activesheet.Sort.SortFields.Clear

is this correct? and also for the other lines

Yes.
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,020
Members
448,543
Latest member
MartinLarkin

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