VBA to sort different tables with same headers

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a code to sort data in multiple tables, each table has the same headers. The issue I am having is that every week I create a new tab by copying the previous weeks tab which renames the table, all the table header names are the same in each table in the workbook. The code I have references the original table causing a run time error. There is only one table on each sheet. How can I write the code to reference the headers within the table on individual sheets?

This is what I currently have, the original table was named "JobList"

Sub Sort_Spreadsheet()


' Sort to input data from Weekly Spreadsheet


ActiveSheet.ListObjects(1).Sort. _
SortFields.Clear
ActiveSheet.ListObjects(1).Sort. _
SortFields.Add Key:=Range("JobList[Status]"), SortOn:=xlSortOnValues _
, Order:=xlDescending, DataOption:=xlSortNormal
ActiveSheet.ListObjects(1).Sort. _
SortFields.Add Key:=Range("JobList[Proj '#]"), SortOn:=xlSortOnValues _
, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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