VBA & Macro Problems

Stop an Excel VBA QR Code Function from Running Repeatedly

Adam DavisAdam Davis Nov 18, 2026 869 views

Question details

Prevent a VBA routine from repeatedly deleting and inserting QR code images.

Stop an Excel VBA QR Code Function from Running Repeatedly
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet formula calls a VBA user-defined function to create a QR code image from cell data.
Observed behavior
The QR code routine can run again during recalculation or when cells, forms, or other macros are used.
Before you start

Identify the formula that calls the QR code function and the input cell whose value the QR code should represent. Enable the Developer tab if needed, and make sure the workbook is saved in a macro-enabled format such as .xlsm.

Solution 1Recommended

Run the QR code macro only when the input cell changes

Use a Worksheet_Change event instead of a worksheet formula when the QR code should update after an input cell is edited.

Excel can recalculate user-defined functions more often than expected, and worksheet functions should not change the workbook by deleting or inserting pictures. A change event gives the routine a specific trigger.

1
Remove the formula trigger

Delete the worksheet formula that calls the QR code function. Keep the input cell that supplies the QR code data.

2
Prepare the macro

In Developer > Visual Basic, adapt the existing QR code routine into a public Sub named UpdateQRCode in a standard module. Make it read the input cell and update the image without requiring arguments.

3
Add the change event

In the Visual Basic editor, open the code window for the worksheet containing the input cell. Add the following event, replacing A2 with your input cell if necessary: Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Me.Range("A2")) Is Nothing Then Exit Sub On Error GoTo HandleError Application.EnableEvents = False UpdateQRCode CleanUp: Application.EnableEvents = True Exit Sub HandleError: MsgBox "QR code update failed: " & Err.Description Resume CleanUp End Sub

4
Test the trigger

Return to the worksheet and edit A2. Confirm that the QR code updates, then edit an unrelated cell and check that the routine does not run.

Run the QR code macro only when the input cell changes
Formula results need a different trigger: Worksheet_Change does not fire when a formula result changes because of recalculation. If the QR code input is a formula result, run the macro explicitly when appropriate or design a recalculation handler that checks whether the result actually changed. If the sheet already has a Worksheet_Change procedure, incorporate the logic into it rather than adding a second one.
Free Microsoft Office alternative

Work with QR code input data in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Microsoft Office formats, making it easy to move everyday spreadsheet work. It does not fix Excel's VBA recalculation behavior; check macro compatibility before migrating automated workbooks.

Review and edit spreadsheet data used as QR code inputOpen and save Microsoft Excel formats, including XLSXUse a familiar spreadsheet interface for everyday tasksMove routine spreadsheet work to a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my QR code function run when I edit another cell?

Excel may recalculate worksheet formulas after changes elsewhere in the workbook. A VBA function called by a formula can therefore run again even when its QR code input has not changed.

Can a worksheet function insert or delete QR code pictures reliably?

A worksheet user-defined function should return a value rather than change the workbook. Move picture insertion and deletion into a standard macro or a worksheet event procedure.

Will Worksheet_Change detect a recalculated formula result?

No. Worksheet_Change responds to changes to cells, not formula results that change during recalculation. Run the macro explicitly or use a recalculation handler that checks for a changed result.

Where should I put the Worksheet_Change code?

Open Developer > Visual Basic and place it in the code window for the worksheet containing the input cell. If that worksheet already has a Worksheet_Change procedure, add the new logic to the existing procedure.

Why is the QR code macro not running after I edit the input?

Check that macros are enabled, the event code is in the correct worksheet, and its cell reference matches the input. In the Visual Basic editor's Immediate window, check whether Application.EnableEvents is True.