Increment Excel Find and Replace Values Each Time a VBA Macro Runs
Question details
Increment the Find and Replace values by one each time a 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.
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.
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.
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.
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
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.

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.

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.




