Excel VBA Select Current Worksheet Various Names

I have a macro that sorts columns that I need to run on different worksheets in different workbooks.
Each worksheet has a different name. The macro needs to refer to the "Current Worksheet", rather than a specific worksheet name.
Sample Code:

Range("B6").Select
    ActiveWorkbook.Worksheets("Company A").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Company A").Sort.SortFields.Add Key:=Range( _
        "B6:B44"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Company A").Sort.SortFields.Add Key:=Range( _
        "D6:D44"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Company A").Sort
        .SetRange Range("A5:EL44")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

We need to avoid manually renaming each worksheet before running the macro.

Any suggestions would be appreciated.

Tosagua
TosaguaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Change

Worksheets("Company A")

to

Activesheet

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ramesh VCommented:
Hi,
Please test the below code:

Sub Sort_Data()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        ws.Activate
        ws.Sort.SortFields.Clear
        ws.Sort.SortFields.Add Key:=Range("C:C") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ws.Sort.SortFields.Add Key:=Range("F:F") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ws.Sort
            .SetRange Range("A:P")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next ws
End Sub
Ramesh VCommented:
The above code sorts all the worksheets in a workbook and sorts.
The code has 2 levels of sorting, 1st on C and then 2nd on column F. My Data range is from A:P
Change the range according to your requirement
TosaguaAuthor Commented:
Syed,

This is great. I was struggling with complicated solutions, when the best answer was simple.

Thank you.

Tosagua
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.