Utilize Macro On New Worksheet

bmart

New Member
Joined
Apr 18, 2016
Messages
2
I recorded a very simple macro and added a 'button' in order to run it. On the original worksheet, everything functions properly. However, I will be generating a new worksheet and report every week, so I'd like to be able to add the macro to future worksheets. I thought that changing the worksheet name to 'ActiveSheet' would solve the problem. However, I believe I am now running into an issue with the Table Name 'ListObjects' in VBA.

On the original worksheet, the table was named 'Table 14'. However, each subsequent worksheet that I create will have a different table name (Excel requirement).

Does anyone have any recommendation on changes to the following code that will detach the macro from a specific table (object)?

Thanks in advance!

Sub SortCash()
'
' SortCash Macro
'


'
Range("F2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.ActiveSheet.ListObjects("Table14").Sort. _
SortFields.Clear
ActiveWorkbook.ActiveSheet.ListObjects("Table14").Sort. _
SortFields.Add Key:=Range("F2"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.ListObjects("Table14"). _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 

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.
Since I will be running the report weekly, I will keep multiple weeks (ie multiple worksheets) in the same workbook. Thus there will be several tables in the workbook, which is where the problem presents itself.

For example: On worksheet 2, the code would need to read 'ActiveWorkbook.ActiveSheet.ListObjects("Table5").Sort. _SortFields.Clear.....'
 
Upvote 0

Forum statistics

Threads
1,217,367
Messages
6,136,143
Members
449,994
Latest member
Rocky Mountain High

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