Autofill from the last row using an office script in an excel report
This script will look for the last row then it will copy and paste the data to a number of rows below. In fact, it will do like when I do a manual scroll/drag down by copying the selected cells then pasting them below by dragging down my mouse.
When I use the script ?
When I have to extend my data by copying from the last row to numbers of row down below.
How to create the script ?
Read How to create, edit and select an Office Script in an excel report
How to create the button to associate it with the script ?
Read How to create a button and associated it to an Office Script in an excel report
How is/are the script(s) ?
Copy the code below and paste it into your script. You will see my comments in green if exist so follow the help to adapt to your need.
function main(workbook: ExcelScript.Workbook) { // change sheet1 by yours let sheet = workbook.getWorksheet("Sheet1"); let usedLastRow = sheet.getUsedRange().getRowCount(); // change column A and C by yours and 5 by the number of row to paste // change usedLastRow by row number if to insert new rows in a specific row i.e row 4 -> ("A4:C4").autoFill(`A4:C${4 + 5}` sheet.getRange(`A${usedLastRow}:C${usedLastRow}`).autoFill(`A${usedLastRow}:C${usedLastRow + 5}`, ExcelScript.AutoFillType.fillDefault); }
The same result with another script:
function main(workbook: ExcelScript.Workbook) { // change sheet1 by yours let sheet = workbook.getWorksheet("Sheet1"); // change column A and C by yours let usedLastRowa = sheet.getRange("A:A").getUsedRange().getRowCount(); let usedLastRowc = sheet.getRange("C:C").getUsedRange().getRowCount(); // change 5 by the number of row to paste // change usedLastRowa by row range if to insert new rows in a specific row i.e row 4 -> ("A4:C4").autoFill(`A4:C${4 + 5}` sheet.getRange(`${usedLastRowa}:${usedLastRowc}`).autoFill(`${usedLastRowa}:${usedLastRowc + 5}`, ExcelScript.AutoFillType.fillDefault); }
This script is not related to autofill but I share it in case, it copies the last row to the new one (1 new row only):
function main(workbook: ExcelScript.Workbook) { // change sheet1 by yours let sheet = workbook.getWorksheet("Sheet1"); let lastRowAddress = sheet.getUsedRange().getLastCell().getAddress(); let lastRow = sheet.getRange(lastRowAddress).getEntireRow().getUsedRange(); lastRow.getOffsetRange(1, 0).copyFrom(lastRow); }
This script is for a table:
function main(workbook: ExcelScript.Workbook) { // change sheet1 and table1 by yours let sheet = workbook.getWorksheet("Sheet1"); let table = sheet.getTable("Table1"); let countrow1 = table.getRowCount(); let countrow2 = countrow1 + 1; let myarray: (string | number | boolean)[][] = []; // if your table has more than 3 columns, add ,"" i.e 4 column table -> ["", "", "", ""] const tabcolumn = ["", "", ""]; //change 5 by number of row to add for (let i = 0; i < 5; i++) { myarray.push(tabcolumn) } //change countrow1 by row number if to insert new rows in a specific row i.e for row 4 put 3 -> (3, myarray) table.addRows(countrow1, myarray); //change 5 by number of row to add //if row number, replace countrow2 by row number i.e row 4 -> ("A4:C4").autoFill(`A4:C${4 + 5}` sheet.getRange(`A${countrow2}:C${countrow2}`).autoFill(`A${countrow2}:C${countrow2 + 5}`, ExcelScript.AutoFillType.fillDefault); }
Again, not related to autofill, it copies the last row to the new one for a table (1 new row only):
function main(workbook: ExcelScript.Workbook) { // change sheet1 and table1 by yours let sheet = workbook.getWorksheet("Sheet1"); let table = sheet.getTable("Table1"); let lastRowAddress = sheet.getUsedRange().getLastCell().getAddress(); let lastRow = sheet.getRange(lastRowAddress).getEntireRow().getUsedRange(); // if your table has more than 3 columns, add ,"" i.e 4 column table -> [["", "", "", ""]] let tabcolumn = [["", "", ""]]; table.addRows(-1, tabcolumn); lastRow.getOffsetRange(1, 0).copyFrom(lastRow); }
Interesting Topics
-
Be successfully certified ITIL 4 Managing Professional
Study, study and study, I couldn’t be successfully certified without studying it, if you are interested...
-
Be successfully certified ITIL 4 Strategic Leader
With my ITIL 4 Managing Professional certification (ITIL MP) in the pocket, it was time to go for the...
-
Hide visual and change background color based on selection
Some small tricks to customize the background colour of a text box...
-
Stacked and clustered column chart or double stacked column chart
In excel, I use a lot the combination of clustered and stacked chart...