How to Automatically Replace and Resize an Excel Image When Its URL Changes
Question details
The user needs a VBA macro that automatically updates, resizes, and centers an image in Excel when the corresponding image URL in a specific cell changes.

- Product
- Excel 2019
- Device & OS
- not provided
- Scenario
- Updating a list of image URLs in a worksheet and wanting the corresponding images to automatically refresh, delete the old versions, and resize to fit adjacent cells without manual intervention.
- Observed behavior
- Currently, updating a URL requires the user to manually delete the old image, insert the new one from the updated URL, and manually resize it to fit the adjacent cell.
Ensure that you have enabled the Developer tab in your Excel ribbon and that macros are allowed in your workbook Trust Center settings before adding and running the VBA code.
Use Worksheet_Change VBA Macro to Automate Image Replacement
Implement a VBA macro that triggers automatically when a URL cell is modified, instantly deleting the old image and fetching the new one.
This VBA script uses the Worksheet_Change event to monitor a specific range (such as A2:A5). When a URL is updated, it automatically removes the existing image located in the adjacent cell, inserts the new image from the URL, and scales it to fit properly.
To prevent the macro from triggering itself continuously during execution, the code temporarily disables screen updating and uses error handling. If your URLs are generated by formulas rather than manual entry, the Worksheet_Change event will not trigger; you will need to use the Worksheet_Calculate event instead.
Right-click the sheet tab containing your URLs at the bottom of the screen and select 'View Code' to open the Visual Basic for Applications Editor.
Copy and paste the appropriate Worksheet_Change VBA code into the module window. Ensure the code includes logic to loop through ActiveSheet.Shapes to delete any existing picture in the modified row.
Modify the target range in the code (e.g., changing 'Set Rng = ActiveSheet.Range("A2:A5")') to match the exact cells containing your image URLs.
Adjust the scaling properties like '.ScaleHeight 0.5' and '.ScaleWidth 0.5' if you need a specific dimension for your images, or use the cell's Width and Height properties to calculate exact fits.
Close the VBA Editor and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) so the script remains active for future sessions.
Test the macro by pasting a valid, direct image URL into one of the designated cells. The image should appear, scale, and center in the adjacent cell automatically.

Automate Image Resizing with Macros in WPS Spreadsheet
WPS Office Spreadsheet provides robust support for VBA macros, allowing you to run automated scripts like automatic image replacement seamlessly. You can easily access the Developer tools to customize your worksheets and streamline your data processes.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the document where you want to automate image replacement.
- 2. Access Developer Tools: Go to the 'Developer' tab on the top ribbon. If it's not visible, enable it via Menu > Options > Customize Ribbon.
- 3. Launch the VB Editor: Click on the 'VB Editor' button to open the Visual Basic for Applications window.
- 4. Insert the Code: Double-click the relevant Sheet name in the Project Explorer and paste your Worksheet_Change code into the text area.
- 5. Save as XLSM: Go to File > Save As, and choose 'Microsoft Excel Macro-Enabled Workbook (*.xlsm)' to preserve your automation.

Frequently Asked Questions
Why doesn't the macro trigger when the URL changes via a formula?
The Worksheet_Change event is only triggered when a user manually edits a cell or when a script explicitly changes its value. For changes driven by formula calculations, you must use the Worksheet_Calculate event instead.
How do I adjust the size of the inserted image in the VBA code?
You can adjust the size by modifying the .ScaleHeight and .ScaleWidth values inside the With block of your code. For instance, changing '0.5' to '1.0' will display the image at 100% of its original resolution.
Will the macro accidentally delete other charts or shapes on the worksheet?
The provided logic checks if the 'TopLeftCell.Row' of a shape matches the modified cell's row before deleting it. While it attempts to target only the corresponding image, any other unrelated shape that happens to share the exact same top-left row might also be deleted. Naming your picture objects explicitly can help prevent this.
Why am I getting a 'Run-time error' when inserting the picture?
This usually occurs if the URL is invalid, the image format is unsupported, or your computer lacks an active internet connection to download the file. Ensure the URL is a direct link to an image file ending in extensions like .jpg or .png.




