VBA & Macro Problems

Excel for the Web: Clear Cells Across Multiple Worksheets with Office Scripts

Maira MehtabMaira Mehtab Nov 12, 2026 868 views

Question details

The user needs to clear selected cells on multiple worksheets in an Excel workbook.

Product
Excel for the web and Office Scripts
Device & OS
not provided
Scenario
The workbook contains target cells on several worksheets, and the user wants to clear their contents in one automated operation.
Observed behavior
Traditional VBA macros cannot run in Excel for the web, so the task must be completed with an Office Script from the Automate tab.
Before you start

Confirm the exact worksheet names and cell addresses you want to clear. Open the workbook in Excel for the web and verify that the Automate tab and Office Scripts are available for your account.

Solution 1Recommended

Use an Office Script to clear selected cells across worksheets

Use this solution when Excel for the web must clear specific cells on several worksheets without removing their formatting.

The script stores each worksheet name and cell address in an array, then clears only the cell contents. The worksheet names and addresses must match the workbook exactly; otherwise, the script may fail or target the wrong location.

1
Open the Automate tab

Open the workbook in Excel for the web, select the Automate tab on the ribbon, and choose New Script to open the Office Scripts code editor.

2
Replace the sample script

Delete the existing code and enter this script, updating the worksheet names and cell addresses as needed: function main(workbook: ExcelScript.Workbook) { const cellsToClear = [{sheetName: "Sheet1", cellAddress: "A1"}, {sheetName: "Sheet2", cellAddress: "B5"}, {sheetName: "Sheet3", cellAddress: "C10"}]; for (const cellInfo of cellsToClear) { const sheet = workbook.getWorksheet(cellInfo.sheetName); sheet.getRange(cellInfo.cellAddress).clear(ExcelScript.ClearApplyTo.contents); } }

3
Save and run the script

Select Save script, give it a recognizable name, and select Run from the Office Scripts editor or the Automate tab.

4
Verify the worksheet results

Open each listed worksheet and confirm that the specified cells are empty while their formatting remains unchanged.

Contents only are cleared: ExcelScript.ClearApplyTo.contents removes values and formulas but preserves the cells' formatting.
Free Microsoft Office alternative

Switch to WPS Spreadsheet for Familiar Multi-Sheet Editing

Office Scripts are a Microsoft Excel for the web automation feature and cannot be run directly in WPS Office. For routine spreadsheet editing, WPS Spreadsheet offers a free, lightweight alternative with a familiar interface, strong Microsoft Excel format compatibility, and an easy migration path.

Free and lightweight spreadsheet editing for everyday workbook tasksFamiliar spreadsheet interface for managing multiple worksheets and selected cellsCompatible with Microsoft Excel XLS and XLSX formats for easier file migrationUseful tools for editing, clearing, formatting, and organizing spreadsheet data
microsoft office alternative - wps office

Frequently Asked Questions

Can Excel for the web run a traditional VBA macro to clear cells?

No. Excel for the web does not run traditional VBA macros. Use an Office Script from the Automate tab instead.

Does the Office Script remove cell formatting?

No. The command clear(ExcelScript.ClearApplyTo.contents) clears values and formulas while preserving formatting.

What should I check if the script cannot find a worksheet?

Compare the sheetName values in the script with the worksheet tabs in the workbook. Names must match exactly, including spaces and punctuation.

Can I clear a range instead of one cell?

Yes. Replace a single-cell address such as A1 with a range address such as A1:C10 in getRange(). The script will clear the contents of that entire range.