Stop an Excel VBA QR Code Function from Running Repeatedly
Question details
Prevent a VBA routine from repeatedly deleting and inserting QR code images.

- 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.
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.
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.
Delete the worksheet formula that calls the QR code function. Keep the input cell that supplies the QR code data.
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.
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
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 manually
Use a standard macro when the QR code only needs to be refreshed on demand.
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.

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.




