Excel online script for adding multiple new rows

Copper Contributor

Hello everyone, I'm wondering if there is a way to create a Script that will add one or more rows at the users desired location? I know this is a very simple thing to do in excel anyway but this might help some of my colleagues who struggle with excel. I managed to do this with a VBA which worked great but that doesn't seem to work on shared documents in the online version of Excel. Would a script be able to do this? Thanks

4 Replies
Hello @James_Furmage
I am not sure VBA is implemented in the Online version of MS-Excel. I know a version of it is being worked on, but that is for the future. Look at this: https://support.microsoft.com/en-us/office/work-with-vba-macros-in-excel-for-the-web-98784ad0-898c-4...

maybe you can add the code to the PERSONAL workbook, and run it that way but I am not sure if even that would work.

Hi @James_Furmage,

 

Thanks for reaching out and apologies for the delayed response! You could create an Office Script with the following code and attach it to a button so that your colleagues can easily use it:

 

function main(workbook: ExcelScript.Workbook) {
	let selectedCell = workbook.getActiveCell();
	selectedCell.getEntireRow().insert(ExcelScript.InsertShiftDirection.down);
}

 

 This script will insert a row above the selected cell. Hopefully it helps - let me know if you have any questions!

 

Best,

Michelle

@MichelleRanMSFT 

Thanks for this mate, this is what I was after looked high and low looking all over the internet.

Just wondering if there is a way that from what you have said you can also able to copy the first column in that row to the row that you are freshly inserting?

Cheers

@EdwardJ-RITS this script should insert a new row under the selected cell and copy the value from the first column:

function main(workbook: ExcelScript.Workbook) {
    let selectedRow = workbook.getActiveCell().getEntireRow();
    let newRow = selectedRow.getEntireRow().insert(ExcelScript.InsertShiftDirection.down);
    newRow.getCell(0, 0).setValue(selectedRow.getCell(0, 0).getValue());
}

Let me know if that helps, or if you have any questions!