Google Sheets Macro - Add Pause between Functions

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
Platform
  1. Windows
I have a Macro that is activated by a button in my Sheets. It does exactly what I want it to do separated, but when I tried to combine both functions (Autofill & Sort), it doesn't finish the sort. My guess is that it is doing it all simultaneously so it's sorting before the autofill calculation is actually completed/displayed. Any way to adjust this macro so it does the Autofill and then the Sort?

Code:
function Sorting() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('N3:Q3').activate();
  spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('N3:Q'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getRange('A3:Q').activate()
  .sort({column: 17, ascending: false});
};
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I do not use Google Sheets, but I know there are many similarities between Excel and Google Sheets.
If functions work the same, functions can only return values to the cell they are located in, like any other built-in function that you use in formulas.
So I don't think you can do "Sorting" via a Function. That would need to be placed in a Procedure.

Note that you could both autofill and sort within a Procedure, as procedure can populate cells as well as format, perform steps, etc.
 
Upvote 0
I do not use Google Sheets, but I know there are many similarities between Excel and Google Sheets.
If functions work the same, functions can only return values to the cell they are located in, like any other built-in function that you use in formulas.
So I don't think you can do "Sorting" via a Function. That would need to be placed in a Procedure.

Note that you could both autofill and sort within a Procedure, as procedure can populate cells as well as format, perform steps, etc.

Thanks for your quick response! Both the Sorting and AutoFill functions worked when they were separated and called upon by different triggers (buttons). It's only when I tried to combine them that it doesn't want to work. If they cannot be combined then I can realistically just create a button for each function, just thought it would be nicer with only 1.

Below are the 2 functions separated;
Code:
function Sorting() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A3:Q').activate()
  .sort({column: 17, ascending: false});
};

function AutofillRange() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('N3:Q3').activate();
  spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('N3:Q'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getRange('N3:Q').activate();
};
 
Upvote 0
Below is the working code.

Code:
function Sorting() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('N3:Q3').activate();
  spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('N3:Q'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getRange('N3:Q').activate();
  SpreadsheetApp.flush()
  spreadsheet.getRange('A3:Q').activate()
  .sort({column: 17, ascending: false});
};
 
Upvote 0
Solution

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top