How can I execute one Google Sheets macro from within another macro?

I’m working with Google Sheets and trying to run one macro from inside another macro. My main macro is supposed to execute until it hits a conditional statement, and when that condition is met, it should trigger a second macro to run.

Here’s what I’m attempting to do:

worksheet.getCurrentCell().offset(0, 0, 45, 3).activate();
worksheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
worksheet.getActiveRange().If Cell('C2').value == 15, Then Execute MyFunction;

I want the macro called ‘MyFunction’ to execute when the condition is true, then continue with the remaining lines of my original macro. However, I keep getting a syntax error that says ‘Missing ; before statement’ on line 16 in my macros file. What’s the correct way to call another macro from within a macro in Google Sheets?

That syntax won’t work - Google Apps Script uses JavaScript, so you need proper conditional statements. Replace that line with standard JS syntax:

if (worksheet.getRange('C2').getValue() == 15) {
  MyFunction();
}

Just make sure MyFunction is defined in the same script file or imported if it’s elsewhere. Function calls are just the name + parentheses. I’ve used this setup tons of times in sheet automation and it’s rock solid once the syntax is right. Your macro will run MyFunction when the condition hits, then move on to whatever’s next.

yeah, that’s definitely a syntax issue. you can’t mix pseudo-code like that in Google Scripts. try this instead: if(worksheet.getRange('C2').getValue() === 15){ MyFunction(); } the triple equals is better for strict comparison. also make sure MyFunction() is declared somewhere in your script file or it’ll throw an error when called.

You’re mixing pseudocode with actual JavaScript syntax. Google Apps Script doesn’t understand ‘If Cell… Then Execute’ statements - it uses standard JavaScript conventions. You need to completely rewrite that line. Don’t access the cell through the worksheet object every time. Store the cell value in a variable first: var cellValue = worksheet.getRange('C2').getValue(); then use a simple if statement. This makes your code cleaner and faster, especially if you’re referencing that cell multiple times. I always break complex operations into smaller chunks - it helps avoid these syntax issues with Google Sheets macros. Once you fix the conditional logic, calling your secondary function is easy.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.