VBA & Macro Problems

Increment Excel Find and Replace Values Each Time a VBA Macro Runs

Ayan MasoodAyan Masood Dec 2, 2026 868 views

Question details

Increment the Find and Replace values by one each time a macro runs.

Increment Find and Replace Values Each Time an Excel Macro Runs
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user runs a macro, potentially from a button, to replace a number on a worksheet and wants the next run to use the next number.
Observed behavior
Each run should use the stored last Find value to calculate the next Find value and its replacement.
Before you start

Confirm that the data sheet is named Nightly, or adjust the name in the code. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and enable macros if prompted.

Solution 1Recommended

Store the Last Find Value in a Control Sheet

Use a control cell to preserve the last Find value between macro runs and target the Nightly sheet explicitly.

1
Create the control cell

Add a worksheet named Hidden. In cell A1, enter the last number used as the Find value. For example, enter 9 to make the next run find 10 and replace it with 11.

2
Add the macro

Open the VBA editor with Alt+F11, choose Insert > Module, and paste this code: Sub NightlySheet() Dim v As Long v = ThisWorkbook.Worksheets("Hidden").Range("A1").Value + 1 ThisWorkbook.Worksheets("Nightly").Cells.Replace _ What:=v, Replacement:=v + 1, LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False ThisWorkbook.Worksheets("Hidden").Range("A1").Value = v End Sub

3
Run and save

Run NightlySheet from the Macros dialog or assign it to a button. Save the workbook after running it so the updated control value is retained for the next session. You can hide the Hidden sheet by right-clicking its tab and choosing Hide.

Store the Last Find Value in a Control Sheet
Check the replacement scope: The macro searches all cells on Nightly and advances the counter even if it finds no match. With xlPart, a number can also match part of a longer value or formula; use xlWhole if you need whole-cell matches. Later runs may replace values produced by earlier runs.
Free Microsoft Office alternative

Work With Excel Spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and Excel format compatibility for an easier move from Microsoft Office. It does not fix Excel VBA issues, and macro support may vary by version.

Find and replace spreadsheet content through a familiar interfaceOpen and edit Microsoft Excel formats such as XLSXUse a lightweight, easy-to-navigate Office suiteMove existing spreadsheets into a familiar workspace
microsoft office alternative - wps office

Frequently Asked Questions

What number should I put in Hidden!A1?

Enter the last number used as the Find value. If you enter 9, the next run finds 10 and replaces it with 11.

How do I make the macro replace only complete cell values?

In the VBA code, change LookAt:=xlPart to LookAt:=xlWhole. The macro will then look for whole-cell matches rather than matching part of a value.

Will the counter advance if nothing is found?

Yes. The example writes the new Find value to Hidden!A1 after the Replace call, even when there is no match.

How can I run the macro from a worksheet button?

On Excel's Developer tab, choose Insert and add a button, then assign the NightlySheet macro when prompted. Save the workbook as an .xlsm file.