I recorded a small macro where I take a table that was loaded from Power Query and format it to where there are not empty cells and I have the information I need. All I did was copy and past values from different cells into other cells and then made some format changes at the end like borders, font size, and etc.
It worked for my first tab but when I tried to run the macro for the next tab of unorganized data, I got that error message.
This is the line of code in question:
selectedSheet.getRange("A1").copyFrom(table_INV_1020845.getColumn("Column1").getRangeBetweenHeaderAndTotal().getRow(0), ExcelScript.RangeCopyType.all, false, false);
I tried the advice from this post to no avail. I need this macro to work for hundreds of other tabs of data that is exactly identical except for cell values.
Adding the complete code for clarity:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getWorksheets()[0];
let table_INV_1020845 = workbook.getTable("Table_INV_1020845");
// Paste to range A1 on selectedSheet from table table_INV_1020845 cell in row 0 on column Column1
selectedSheet.getRange("A1").copyFrom(table_INV_1020845.getColumns()[0].getRangeBetweenHeaderAndTotal().getRow(0), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 0 on column STYLE from table table_INV_1020845 cell in row 1 on column STYLE
table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(0).copyFrom(table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(1), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 1 on column STYLE from table table_INV_1020845 cell in row 5 on column STYLE
table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(1).copyFrom(table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(5), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 2 on column STYLE from table table_INV_1020845 cell in row 10 on column STYLE
table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(2).copyFrom(table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(10), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 3 on column STYLE from table table_INV_1020845 cell in row 20 on column ^{Invoice #} 1020845
table_INV_1020845.getColumn("STYLE").getRangeBetweenHeaderAndTotal().getRow(3).copyFrom(table_INV_1020845.getColumn("^{Invoice #} 1020845").getRangeBetweenHeaderAndTotal().getRow(20), ExcelScript.RangeCopyType.all, false, false);
// Set range A5 on selectedSheet
selectedSheet.getRange("A5").setValue("TOTAL");
// Paste to range B1 on selectedSheet from table table_INV_1020845 cell in row 0 on column Column13
selectedSheet.getRange("B1").copyFrom(table_INV_1020845.getColumn("Column13").getRangeBetweenHeaderAndTotal().getRow(0), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 0 on column EA from table table_INV_1020845 cell in row 1 on column Column13
table_INV_1020845.getColumn("EA").getRangeBetweenHeaderAndTotal().getRow(0).copyFrom(table_INV_1020845.getColumn("Column13").getRangeBetweenHeaderAndTotal().getRow(1), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 1 on column EA from table table_INV_1020845 cell in row 5 on column Column13
table_INV_1020845.getColumn("EA").getRangeBetweenHeaderAndTotal().getRow(1).copyFrom(table_INV_1020845.getColumn("Column13").getRangeBetweenHeaderAndTotal().getRow(5), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 2 on column EA from table table_INV_1020845 cell in row 10 on column Column13
table_INV_1020845.getColumn("EA").getRangeBetweenHeaderAndTotal().getRow(2).copyFrom(table_INV_1020845.getColumn("Column13").getRangeBetweenHeaderAndTotal().getRow(10), ExcelScript.RangeCopyType.all, false, false);
// Paste to range C1 on selectedSheet from table table_INV_1020845 cell in row 0 on column Column12
selectedSheet.getRange("C1").copyFrom(table_INV_1020845.getColumn("Column12").getRangeBetweenHeaderAndTotal().getRow(0), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 0 on column QTY from table table_INV_1020845 cell in row 1 on column Column12
table_INV_1020845.getColumn("QTY").getRangeBetweenHeaderAndTotal().getRow(0).copyFrom(table_INV_1020845.getColumn("Column12").getRangeBetweenHeaderAndTotal().getRow(1), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 1 on column QTY from table table_INV_1020845 cell in row 5 on column Column12
table_INV_1020845.getColumn("QTY").getRangeBetweenHeaderAndTotal().getRow(1).copyFrom(table_INV_1020845.getColumn("Column12").getRangeBetweenHeaderAndTotal().getRow(5), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 2 on column QTY from table table_INV_1020845 cell in row 10 on column Column12
table_INV_1020845.getColumn("QTY").getRangeBetweenHeaderAndTotal().getRow(2).copyFrom(table_INV_1020845.getColumn("Column12").getRangeBetweenHeaderAndTotal().getRow(10), ExcelScript.RangeCopyType.all, false, false);
// Set range A1 on selectedSheet
selectedSheet.getRange("A1").setValue("^{Invoice #} 1020845");
// Set range D1:D5 on selectedSheet
selectedSheet.getRange("D1:D5").setValues([["Invoice #"],["-"],["-"],["-"],[1020845]]);
// Paste to table table_INV_1020845 cell in row 3 on column QTY from table table_INV_1020845 cell in row 20 on column EA
table_INV_1020845.getColumn("QTY").getRangeBetweenHeaderAndTotal().getRow(3).copyFrom(table_INV_1020845.getColumn("EA").getRangeBetweenHeaderAndTotal().getRow(20), ExcelScript.RangeCopyType.all, false, false);
// Paste to table table_INV_1020845 cell in row 3 on column EA from table table_INV_1020845 cell in row 20 on column Column5
table_INV_1020845.getColumn("EA").getRangeBetweenHeaderAndTotal().getRow(3).copyFrom(table_INV_1020845.getColumn("Column5").getRangeBetweenHeaderAndTotal().getRow(20), ExcelScript.RangeCopyType.all, false, false);
// Delete range E:O on selectedSheet
selectedSheet.getRange("E:O").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range 6:23 on selectedSheet
selectedSheet.getRange("6:23").delete(ExcelScript.DeleteShiftDirection.up);
// Set font size to 12 for all cells on selectedSheet
selectedSheet.getRange().getFormat().getFont().setSize(12);
// Set horizontal alignment to ExcelScript.HorizontalAlignment.center for all cells on selectedSheet
selectedSheet.getRange().getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
selectedSheet.getRange().getFormat().setIndentLevel(0);
// Set vertical alignment to ExcelScript.VerticalAlignment.bottom for all cells on selectedSheet
selectedSheet.getRange().getFormat().setVerticalAlignment(ExcelScript.VerticalAlignment.bottom);
selectedSheet.getRange().getFormat().setIndentLevel(0);
// Set wrap text to false for all cells on selectedSheet
selectedSheet.getRange().getFormat().setWrapText(false);
// Set text orientation to 0 for all cells on selectedSheet
selectedSheet.getRange().getFormat().setTextOrientation(0);
// Indent set to 0 for all cells on selectedSheet
selectedSheet.getRange().getFormat().setIndentLevel(0);
// Set width of column(s) at all cells on selectedSheet to 10.4
selectedSheet.getRange().getFormat().setColumnWidth(10.4);
// Auto fit the columns of all cells on selectedSheet
selectedSheet.getRange().getFormat().autofitColumns();
// Set font bold to true for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getFont().setBold(true);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.diagonalDown).setStyle(ExcelScript.BorderLineStyle.none);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.diagonalUp).setStyle(ExcelScript.BorderLineStyle.none);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeLeft).setStyle(ExcelScript.BorderLineStyle.continuous);
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeLeft).setWeight(ExcelScript.BorderWeight.medium);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeTop).setStyle(ExcelScript.BorderLineStyle.continuous);
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeTop).setWeight(ExcelScript.BorderWeight.medium);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeBottom).setStyle(ExcelScript.BorderLineStyle.continuous);
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeBottom).setWeight(ExcelScript.BorderWeight.medium);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeRight).setStyle(ExcelScript.BorderLineStyle.continuous);
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.edgeRight).setWeight(ExcelScript.BorderWeight.medium);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.insideVertical).setStyle(ExcelScript.BorderLineStyle.none);
// Set border for 1 row(s) at index 3 on table table_INV_1020845
table_INV_1020845.getRangeBetweenHeaderAndTotal().getRow(3).getFormat().getRangeBorder(ExcelScript.BorderIndex.insideHorizontal).setStyle(ExcelScript.BorderLineStyle.none);
}
I have also added screenshots showing what the output should be and another screenshot of what the table looks like before running the macro so you can see which cells I am trying to change and what the format of the other invoices will look like.
The table before editing anything. I only need a few pieces of information from this table, and the hundreds of others that have this exact look, which is why I recorded the macro.
Desired output.