VBA : Insert Pictures in Multiple Worksheet At Once

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,089
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi expert....

this code work properly only for 3 pictures to inserted into 3 cells, but i want to modify how this code below work in n pictures (multiple pictures) then inserted only in cell H10 for each sheet..

Code:
Sub InsertPic()    
    Dim I As Long
    Dim xPath As String
    Dim xShape As Shape
    Dim xRg As Range
    
    Dim xFiles
    Dim j   As Long
    Dim a   As Range
    
    xPath = "L:\test\"
    xFiles = Array("1.jpg", "2.jpg", "3.jpg")
    
    If Dir(xPath & "*.jpg") = "" Then
        MsgBox "Picture file was not found in path!", vbInformation, "test"
        Exit Sub
    End If
    
    For I = 1 To ActiveWorkbook.Sheets.Count
        Set xRg = Sheets(I).Range("B10:C10,D10:E10,f10:g10")
        For j = 1 To xRg.Areas.Count
            Set a = xRg.Areas(j)
            Set xShape = Sheets(I).Shapes.AddPicture(xPath & xFiles(j - 1), True, True, a.Left, a.Top, a.Width, a.Height)
        Next
    Next


End Sub

expected result :
example :
1.jpg----inserted in SheetA--cell H10
2.jpg----inserted in SheetB--cell H10
3.jpg----inserted in SheetC--cell H10
100.jpg---inserted in SheetXX--cell H10
etc...

.sst
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Based on your Code, Try this:-
NB:- Change ColumnWidth/RowHeight in Code to suit.

Code:
[COLOR="Navy"]Sub[/COLOR] InsertPic()        
    [COLOR="Navy"]Dim[/COLOR] I [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] xPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] xShape [COLOR="Navy"]As[/COLOR] Shape
    [COLOR="Navy"]Dim[/COLOR] a  [COLOR="Navy"]As[/COLOR] Range
    
    xPath = "L:\test\"
    
    [COLOR="Navy"]If[/COLOR] Dir(xPath & "*.jpg") = "" [COLOR="Navy"]Then[/COLOR]
        MsgBox "Picture file was not found in path!", vbInformation, "test"
        [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
    [COLOR="Navy"]End[/COLOR] If
    
    [COLOR="Navy"]For[/COLOR] I = 1 To ActiveWorkbook.Sheets.Count
        [COLOR="Navy"]Set[/COLOR] a = Sheets(I).Range("H10")
            a.RowHeight = 100
            a.ColumnWidth = 20
            [COLOR="Navy"]Set[/COLOR] xShape = Sheets(I).Shapes.AddPicture(xPath & I & ".jpg", True, True, a.Left, a.Top, a.Width, a.Height)
    [COLOR="Navy"]Next[/COLOR]

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top