VBA for sort code, troubleshooting

Hellcheat

New Member
Joined
Nov 10, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi, I've been trying to get some sort code to work but I got stuck.

The idea of the code is that it will be part of a "re-apply" button that will sit in a workbook that contains over 50 individual worksheets with their own tables.
From what I have understood so far to use the ListObject I would need to reference the table I want but considering there's over 50 of them...I want the an easier way out basically.

Is there a way that I can modify the code to act on whatever table exists on the worksheet that the button is pressed?
I should also probably mention this code goes into a bigger one, but I am just posting the section I am having issues with.

VBA Code:
Sub SortExample()

    ActiveSheet.Sort. _
        SortFields.Clear
    ActiveSheets.Sort.SortFields.Add(Range("B38"), _
        xlSortOnCellColor, xlDescending, , _
        xlSortNormal).SortOnValue.Color = RGB(174, 170, 170)
    ActiveSheet.Sort.SortFields.Add Key:=Range("B38"), _
        SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("B38").CurrentRegion
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Really appreciate any help,
Thank you
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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