Filtering using a macro

Competition

New Member
Joined
Sep 19, 2011
Messages
34
Hi
I've tried finding this answer in here, but have had no luck, I have created a macro to cut and paste large amount of raw data into a spreadsheet. Once that is completed I want to filter the data on very specific criteria. However the catch is that I don't want the filter attached to one specific worksheet. I want it to apply to what ever worksheet is open at the time.

Here is the code:

ActiveWorkbook.Worksheets("Sept 26").AutoFilter.Sort.SortFields.Clear

ActiveWorkbook.Worksheets("Sept 26").AutoFilter.Sort.SortFields.Add _Key:=Range("A3:A2832"), SortOn:=xlSortOnValues, Order:=xlAscending, _DataOption:=xlSortNormal

ActiveWorkbook.Worksheets("Sept 26").AutoFilter.Sort.SortFields.Add _Key:=Range("G3:G2832"), SortOn:=xlSortOnValues, Order:=xlDescending, _DataOption:=xlSortNormal

ActiveWorkbook.Worksheets("Sept 26").AutoFilter.Sort.SortFields.Add _Key:=Range("J3:J2832"), SortOn:=xlSortOnValues, Order:=xlDescending, _DataOption:=xlSortNormal

With ActiveWorkbook.Worksheets("Sept 26").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply

End With

Basically I want to remove the reference to Sept 26... Can anyone help?


Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to the board...

I think you could replace all instances of
ActiveWorkbook.Worksheets("Sept 26")
with
ActiveSheet


Although, personal opinion here...
You're better off specifying the sheet..
Making your code dependant on which sheet is active is just asking for trouble.
You never know when the currently active sheet is NOT what you think it is....

Just opinion...



Woo Hoo, Post #20,000 !!!!
 
Upvote 0
Agreed but in this case i need to make an exception :)... Thanks for the correction it works perfectly. Thanks for the warning as well, I'll definitely be careful.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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