Macro - use on one current worksheet (i.e generic name ref)

1cyril1

Board Regular
Joined
Mar 31, 2003
Messages
89
This is likely to be an easy challenge for most of you experts: I have a very helpful macro which I generated via "Record Macro". I want to be able to run it on ANY similarly structured worksheet. Alas, it appears to have the original sheet name embedded (see code below). Can I replace the sheet name (REPORT-Q1-2013) to have it auto reference whatever sheet I happen to be running it in i.e a generic sheet ref? Appreciate any help

End With
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Rows("2:67").Select
ActiveWindow.SmallScroll ToRight:=3
ActiveWorkbook.Worksheets("REPORT-Q1-2013").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("REPORT-Q1-2013").Sort.SortFields.Add Key:= _
Range("E2:E67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("REPORT-Q1-2013").Sort.SortFields.Add Key:= _
Range("AB2:AB67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("REPORT-Q1-2013").Sort
.SetRange Range("A2:AF67")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try the below (untested) code. You generally don't need to select ranges to perform actions on them, and the macro recorder, while useful, generates a lot of redundant code.

Code:
With ActiveSheet
    With .Sort
        .SortFields.Clear
        .Sort.SortFields.Add Key:= _
        .Range("E2:E67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        .Sort.SortFields.Add Key:= _
        .Range("AB2:AB67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("A2:AF67")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With
End Sub
 
Upvote 0
Thanks njimack. I tried replacing my code with yours and hit a compile error "Expected End With" with 'End Sub' highlighted as below. The very top of the (full) macro starts with 'Sub' and macro name on Line1; I guess this is what it's linking back to. Apart from commenting out the replaced code and swopping yours in I didn't change any code. You may spot a glitch in below?

Finally, while I think of it, if this routine is limited to a range of rows I may be in trouble as other worksheets could have more (or less) rows! Probably better to select all rows and get the empty ones to the bottom - is that easy to provide for in the macro?


With ActiveSheet
With .Sort
.SortFields.Clear
.Sort.SortFields.Add Key:= _
.Range("E2:E67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
.Sort.SortFields.Add Key:= _
.Range("AB2:AB67"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A2:AF67")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
Range("C2").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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