Excelscript - dynamic table reference

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm having a bash with the automate features on the online version of Excel.

I have a table from a report where I'm trying to sort column K from oldest to newest. This is nice and easy to do, however, I would like to use the same script (it also has a few extra features like hiding unnecessary columns but there's no need to show that here) on other reports. Each report has the same format and the table is the same too.

I initially thought I could simply include a section of the code that changes the table name before sorting, but this, as shown, references the original name to do so. Is there a way of dynamically acquiring this table name so I can change it/not need to change it in the first place?

Code for changing the name:

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let all_Act_19_11 = workbook.getTable("All_Act_19_11");
    // Rename table all_Act_19_11 to Table
    all_Act_19_11.setName("Table");
}

Code for sorting the column with original name:

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let table = workbook.getTable("Table");
    // Sort on table: table column index: '10'
    table.getSort().apply([{key: 10, ascending: true}]);
}

I'm also not adverse to a VBA solution - the automate function, however, does seem to be easier to share between multiple reports as and when they roll in

Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,214,919
Messages
6,122,260
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