How to Insert OneDrive Images into Excel Cells Using a VBA Macro
Question details
The user needs to automate the process of downloading images from OneDrive hyperlinks and inserting them into specific cells within an Excel worksheet using VBA.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating bulk image insertion from OneDrive links into a structured spreadsheet report or database.
- Observed behavior
- Images need to be dynamically downloaded, placed into designated target cells, and resized to fit perfectly within those cells.
Ensure you have direct-download links for your OneDrive images, not just standard sharing links, as VBA requires direct access to the actual image file to insert it properly.
Create a VBA Macro to Insert and Resize Images
Use this VBA script to read your OneDrive image links and automatically insert and resize the images to fit your target cells.
By leveraging the Pictures.Insert method in VBA, you can fetch an image via a URL and map its dimensions strictly to the dimensions of a target cell. This is highly useful for creating catalogs or visual dashboards.
Ensure your images are uploaded to OneDrive and obtain a sharing link. You must convert these standard sharing links into direct-download URLs so the macro can read the raw image files.
Go to File > Options > Customize Ribbon. Check the box for Developer in the right pane, and click OK to make the Developer tab visible.
Press Alt + F11 on your keyboard to launch the VBA Editor. Next, click Insert > Module from the top menu to create a new standard module for your code.
Copy and paste your macro code into the module window. Ensure the code uses 'ThisWorkbook.Sheets("Sheet1").Pictures.Insert(imgURL)' and sizes the image by setting the picture's .Left, .Top, .Width, and .Height properties to match the target cell's properties.
To preserve your macro code, go to File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) from the format dropdown menu.
Automate Tasks Smoothly with WPS Spreadsheet Macros
WPS Office provides robust support for VBA macros, allowing you to run image insertion scripts without compatibility issues. With an intuitive interface, you can easily access Developer tools to fully automate repetitive spreadsheet tasks.
- 1. Open your Workbook in WPS: Launch WPS Spreadsheet and open your existing Excel Macro-Enabled Workbook (.xlsm).
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon. If it's not visible, go to Menu > Options > Customize Ribbon to enable it.
- 3. Run the Macro: Click on 'Macros', select your image-insertion script from the list, and click 'Run' to populate your spreadsheet cells with images.

Frequently Asked Questions
Why is my VBA macro showing a broken image link instead of the picture?
This typically happens if the OneDrive link is a standard sharing link rather than a direct-download link. The URL must bypass the OneDrive web viewer and point directly to the raw image file for the macro to download it.
How do I fit the inserted image exactly into the cell dimensions?
In your VBA script, you can align the image perfectly to the cell by capturing the target cell as a variable, then setting the inserted picture's .Left, .Top, .Width, and .Height properties to match cell.Left, cell.Top, cell.Width, and cell.Height.
Can I run this macro on a Mac?
VBA picture insertion directly from web URLs can be restricted or behave differently in Excel for Mac due to Apple's sandboxing and OS-level security. It is highly recommended to run web-fetching macros on a Windows environment.
Do I need to save the file in a specific format after adding the macro?
Yes, standard .xlsx files cannot store VBA code. You must save your document as an Excel Macro-Enabled Workbook (.xlsm) to ensure your script is retained after closing the file.




