Forum Discussion

RavinduEB's avatar
RavinduEB
Copper Contributor
Mar 14, 2025

need to create a macro to load images to cells which located in one drive

in column "i" and and column "P" i have linked hyperlink for images. when clicking on command button i need to load images to cells in columns "J" and "Q".

 

https://eswaranlk-my.sharepoint.com/:x:/g/personal/ravindu_eswaran_com/ETYim_DeFBNAup42XJCSDHIBi6d_hq4fKKMTGq00j-AYUA?e=7XGrUu

 

can anyone help me to arrange a macro

4 Replies

  • CrosbyMarlin's avatar
    CrosbyMarlin
    Bronze Contributor

    1. Prepare OneDrive image link: Make sure the image has been uploaded to OneDrive and get the sharing link for the image.
    Convert the link to a direct download link
    2. Enable Developer Tools: Open Excel, click “File” > “Options” > “Customize Ribbon”.
    In the “Main Tab” check the “Development Tools”, click “OK”.
    3. Writing VBA macros: Open Excel, press Alt + F11 to open the VBA editor.
    Insert a new module click “Insert” > “Module”.
    Enter the following code:
    vba
    Sub InsertImageFromOneDrive()
        Dim imgURL As String
        Dim img As Picture
        Dim cell As Range
            ' Get the direct download link for the image from OneDrive
        imgURL = “https://your-onedrive-direct-download-link.jpg”
           ' Select the target cell
        Set cell = ThisWorkbook.Sheets(“Sheet1”).Range(“A1”)
            ' Insert the image
        Set img = ThisWorkbook.Sheets(“Sheet1”).Pictures.Insert(imgURL)
           ' Resize the picture to fit the cell
        With img
            .Left = cell.Left
            .Top = cell.Top
            .Width = cell.Width
            .Height = cell.Height
        End With
    End Sub
    Replace imgURL with the direct download link for your OneDrive image.
    Replace cell with a reference to the target cell.
    4. Run the macro: Return to Excel and press Alt + F8 to open the Macro dialog box.
    Select InsertImageFromOneDrive macro and click “Run”.
    5. Batch Insert Image: If you need to batch insert multiple images, you can store the image link in a column in Excel, and then modify the macro code to traverse the link and insert the image. For example:
    vba
    Sub InsertMultipleImagesFromOneDrive()
        Dim imgURL As String
        Dim img As Picture
        Dim cell As Range
        Dim i As Integer
        
        For i = 1 To 10 ' Assume that the links are stored in A1:A10.
            imgURL = ThisWorkbook.Sheets(“Sheet1”).Range(“A” & i).Value
            Set cell = ThisWorkbook.Sheets(“Sheet1”).Range(“B” & i).
            
            Set img = ThisWorkbook.Sheets(“Sheet1”).Pictures.Insert(imgURL)
            
            With img
                .Left = cell.Left
                .Top = cell.Top
                .Width = cell.Width
                .Height = cell.Height
            End With
        Next i
    End Sub
    6. Save the workbook:
    Save the workbook in macro-enabled format (.xlsm).

    • RavinduEB's avatar
      RavinduEB
      Copper Contributor

      im getting debug in "imgURL = ThisWorkbook.Sheets(“Sheet1”).Range(“A” & i).Value"

       

       

      can you please help me to add this Macro to workbook itself

       

      https://eswaranlk-my.sharepoint.com/:x:/g/personal/ravindu_eswaran_com/ETYim_DeFBNAup42XJCSDHIBi6d_hq4fKKMTGq00j-AYUA?e=Yt77I9

    • RavinduEB's avatar
      RavinduEB
      Copper Contributor

      can you please help me to add this to attached worksheet itself 

      https://eswaranlk-my.sharepoint.com/:x:/g/personal/ravindu_eswaran_com/ETYim_DeFBNAup42XJCSDHIBi6d_hq4fKKMTGq00j-AYUA?e=H02QvR

Resources