Google Sheet script/Macro to return a formula if cell content is deleted

jimbogarner

Board Regular
Joined
Apr 22, 2010
Messages
102
Hi,

I have got an index match formula that returns a name depending on various criteria, however, some of these may need to be manually overwritten - the tab is generally duplicated for each new week, therefore there is a risk someone forgets that there is a hardcoded name in the file which is then wrong for the current week, so I want to create a macro that can be clicked when the week is duplciated to clear all the content from (in the below example) cells C2:E8 and then an auto script to run that that will return the original formula if the cell text is deleted, however, I don't want it part of the macro as there could be mid-week changes where content is deleted, so I don't want the whole macro to be run clearing all data.

IE a simple example (the main file is a lot larger but as an example)
1636379508364.png


the names are generated based on team and station, however, Harry on Thursday is on leave so Clare has been manually typed in there. I want to be able to delete all the cell content for next week but the formulas to the auto add back in?

Thanks,

James
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
shoud be easy enough if you tell us what the formulae are, otherwise we are just guessing!!
 
Upvote 0
sorry this is the formula in one of the cells, however, this will obviously change as the cell changes, IE D19 becomes D20 etc. etc.

=IFERROR(INDEX(Lists!$H$2:$S$100,MATCH($D19,Lists!$H$2:$H$100,0),MATCH(G$16,Lists!$H$1:$S$1,0))," ")

Thanks,

James
 
Upvote 0
I just record this macro using your equation and it seems to work. :
VBA Code:
function test() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('C2').activate()
  .setFormula('=IFERROR(INDEX(Lists!$H$2:$S$100,MATCH($D19,Lists!$H$2:$H$100,0),MATCH(G$16,Lists!$H$1:$S$1,0))," ")');
  spreadsheet.getRange('D2:E2').activate();
  spreadsheet.getRange('C2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  spreadsheet.getRange('C3:C8').activate();
  spreadsheet.getRange('C2:E2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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