Use VBA to Detect Division Formulas and Copy Them in Excel
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.

- 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.
Ensure you have the Developer tab enabled in your spreadsheet ribbon and that macros are permitted to run in your Trust Center settings.
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 ('/').
Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.
In the VBA Editor, click 'Insert' from the top menu and select 'Module' to create a blank script window.
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.
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`.
Add `Application.CutCopyMode = False` to clear the copy selection, then close the logic with `End If`.

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. Download WPS Office: Download and install WPS Office to access the fully-featured Spreadsheet application.
- 2. Enable the Developer Tab: Open WPS Spreadsheet, go to Options, and enable the Developer tab to access Macro tools.
- 3. Paste Your Code: Click 'VBA Editor' on the Developer tab, insert a module, and paste your formula detection script.
- 4. Run the Macro: Select the target cell in your worksheet and click 'Run Macro' to execute the division detection and copy action.

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.




