VBA & Macro Problems

How to Use VBA in WPS Spreadsheets to Replace Formula Text with a Cell Value

John WilsonJohn Wilson Nov 13, 2026 870 views

Question details

The user needs VBA code that replaces a specific text string inside selected formulas with the value from cell A2, limited to the relevant range rather than the whole worksheet.

How to Use VBA in WPS Spreadsheets to Replace Formula Text with a Cell Value
Product
WPS Spreadsheets
Device & OS
not provided
Scenario
The user has formula cells in a workbook and wants to update only the selected formula cells with replacements sourced from cell A2.
Observed behavior
The previous macro replaced text across the entire worksheet like Find and Replace; the user needs a range-limited solution that does not change unrelated cells.
Before you start

Open the workbook in WPS Spreadsheets and make sure the Developer tab is available. Identify the exact target range and the worksheet name, and confirm the replacement source cell, such as A2.

Solution 1Recommended

Replace Text in Selected Formulas with the Value from Cell A2 Using a Range-Limited VBA Macro

Use this method when you need to replace a text string only inside the formulas in the currently selected range, without affecting other cells.

The macro stores the value from cell A2 in a variable and passes it to the Range.Replace method. Because Replace is called on the selected range instead of the whole worksheet, unrelated cells are left unchanged.

1
Open the VBA editor

In WPS Spreadsheets, press Alt+F11 or click Developer > Visual Basic to open the VBA editor.

2
Insert a module

In the VBA editor, click Insert > Module to create a new module for the macro.

3
Paste the range-limited macro

Paste the following code in the module: Sub ReplaceFormulaTextWithCellValue() Dim rng As Range Dim replacementText As String Set rng = Selection replacementText = Range("A2").Value rng.Replace What:="OldText", Replacement:=replacementText, LookAt:=xlPart End Sub

4
Run the macro on the selected range

Return to WPS Spreadsheets, select only the cells that contain the formulas to update, press Alt+F8, choose ReplaceFormulaTextWithCellValue, and click Run.

Replace Text in Selected Formulas with the Value from Cell A2 Using a Range-Limited VBA Macro
Customize the text and range: Change OldText to the text string in your formulas, and update Range("A2") or the worksheet name to match your workbook.

Use a Range-Limited VBA Macro to Replace Formula Text in WPS Spreadsheets

WPS Spreadsheets can run the VBA macro directly, letting you replace formula text in the selected range with the value from cell A2 while keeping other cells unchanged.

  1. 1. Open the workbook: Launch WPS Spreadsheets and open the workbook that contains the formulas to update.
  2. 2. Open the VBA editor: Press Alt+F11 or go to Developer > Visual Basic.
  3. 3. Insert a module and paste the macro: Click Insert > Module, paste the range-limited VBA code, and save the workbook as a macro-enabled file if prompted.
  4. 4. Select the target formulas and run: Select the relevant formula cells, press Alt+F8, choose the macro, and click Run.
Replaces only the selected formula range, leaving unrelated cells intactStores the replacement value from cell A2 instead of hard-coding itUses familiar Range.Replace VBA syntax for easy maintenanceMaintains compatibility with Microsoft Excel .xlsm and .xls macro workbooksRuns in free WPS Office without requiring a paid Microsoft 365 subscription
microsoft office alternative - wps office

Frequently Asked Questions

How do I stop the VBA Replace from changing the entire worksheet?

Assign the target range to a variable, such as Set rng = Selection, and call rng.Replace instead of Cells.Replace or ActiveSheet.Cells.Replace.

How do I use the value from cell A2 as the replacement?

Store it in a string variable: replacementText = Range("A2").Value. Then use Replacement:=replacementText in the Range.Replace method.

Will this macro work in WPS Spreadsheets for an Excel workbook?

WPS Spreadsheets supports VBA macros in many Excel workbooks, including .xlsm files. Open the workbook in WPS Spreadsheets and test the macro because some Excel-specific features may differ.

Why does the replacement value appear blank when I record a macro and copy a cell?

The recorded macro likely uses a hard-coded string or ActiveCell.Paste. Use a variable that reads the source cell value and pass that variable to Replacement instead of recording paste actions.