VBA & Macro Problems

Use VBA to Detect Division Formulas and Copy Them in Excel

Steven ParkerSteven Parker Nov 2, 2026 870 views

Question details

The user needs a VBA macro to inspect a cell, check if its formula contains a division operator, and automatically copy that formula to the cell immediately above it.

Use VBA to Detect Division Formulas and Copy Them in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Writing a VBA script or User Defined Function (UDF) to conditionally duplicate formulas containing division operations based on relative cell references.
Observed behavior
Searching the displayed cell value fails to detect the division symbol because Excel evaluates the mathematical result rather than exposing the underlying formula string.
Before you start

Ensure you have the Developer tab enabled in your spreadsheet ribbon and that macros are permitted to run in your Trust Center settings.

Solution 1Recommended

Use HasFormula and InStr to Detect and Copy Division Formulas

Access the cell's Formula property directly using VBA, search for the division operator, and use the Offset property to paste the formula.

When checking a cell for specific operators, you cannot read its displayed value. You must inspect the actual formula string using the `.Formula` property. Combining `HasFormula` with `InStr` allows you to safely locate the division symbol ('/').

1
Open the VBA Editor

Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the VBA Editor, click 'Insert' from the top menu and select 'Module' to create a blank script window.

3
Write the Detection Logic

Begin your macro and use an If statement to check the formula. Type: `If ActiveCell.HasFormula And InStr(ActiveCell.Formula, "/") > 0 Then` to verify the cell contains a formula with a division symbol.

4
Copy and Offset to the Cell Above

Inside the If statement, copy the cell and paste it to the cell above using: `ActiveCell.Copy` followed by `ActiveCell.Offset(-1, 0).PasteSpecial xlPasteFormulas`.

5
Clear the Clipboard

Add `Application.CutCopyMode = False` to clear the copy selection, then close the logic with `End If`.

Use HasFormula and InStr to Detect and Copy Division Formulas
UDF Declaration and Event Handling: If you are writing a custom User Defined Function instead of a standard Sub macro, ensure you declare the function as `Boolean` so it returns True or False. If you are placing this code inside a `Worksheet_Change` event, temporarily disable events using `Application.EnableEvents = False` before pasting, and turn it back to `True` afterward to prevent an infinite loop.
Advanced Macros in WPS Office

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office fully supports VBA and Excel Macros. You can write, edit, and execute your VBA scripts to detect formulas and automate repetitive tasks directly within WPS Spreadsheet without changing your code.

  1. 1. Download WPS Office: Download and install WPS Office to access the fully-featured Spreadsheet application.
  2. 2. Enable the Developer Tab: Open WPS Spreadsheet, go to Options, and enable the Developer tab to access Macro tools.
  3. 3. Paste Your Code: Click 'VBA Editor' on the Developer tab, insert a module, and paste your formula detection script.
  4. 4. Run the Macro: Select the target cell in your worksheet and click 'Run Macro' to execute the division detection and copy action.
Full compatibility with Microsoft Excel VBA syntax and objectsBuilt-in Developer tools for macro recording, editing, and debuggingSeamless support for opening and saving .xlsm and .xlsb formatsLightweight application with fast code execution
microsoft office alternative - wps office

Frequently Asked Questions

Why does searching the cell value fail to find the division symbol?

When a cell contains a formula like '=10/2', Excel displays the calculated result (5). Searching the active cell's 'Value' will only look at '5'. You must search the cell's 'Formula' property to read the raw text string containing the '/' operator.

How do I copy only the formula without altering the destination cell's formatting?

Instead of using a standard paste command, use the PasteSpecial method with the xlPasteFormulas parameter. In VBA, this is written as `ActiveCell.Offset(-1, 0).PasteSpecial Paste:=xlPasteFormulas`.

Can I modify this VBA script to detect multiplication instead?

Yes. You simply need to change the character you are searching for in the InStr function. Replace the forward slash ("/") with an asterisk ("*") to detect multiplication formulas.