Why does my script only paste the correct copied cells on another page when the script is run from the page of the copied cells that are included in it?

Michelina Avramidis 0 Reputation points
2024-07-25T23:58:48.71+00:00

I would like to copy cells from one page and paste them onto the main ("checklist") page, preferably from a button I assign the macro to on the main page. I automated the copying, inserting and shifting of the cells but it only pastes the correct range when the macro is run from the other page, not the main ("checklist") page. I have added a button onto the other page and it works fine when pressed, I just have to wait until it finishes running before I can go back to the main ("checklist") page. Any help why it doesn't run properly when run from the main ("checklist") page would be much appreciated!

Here is the code:

function main(workbook: ExcelScript.Workbook) {

let checklist = workbook.getWorksheet("Checklist");

let selectedSheet = workbook.getActiveWorksheet();

// Insert copied cells from B10:AU18 on selectedSheet to B4:AU12 on checklist.

checklist.getRange("B4:AU12").insert(ExcelScript.InsertShiftDirection.down);

checklist.getRange("B4:AU12").copyFrom(selectedSheet.getRange("B10:AU18"));

}

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,961 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,986 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.