VBA code to insert image to shape fill

ExcelLee

New Member
Joined
Mar 4, 2021
Messages
7
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
  2. MacOS
Morning all,

I am seeking a VBA code to do the following. I have been trawling the internet trying to sort and also writing my own to no avail.

The document has multiple shapes included named Rectangle 1 through 10. These are to house images.

I am looking for a macro to automatically do the following.

Select the rectangle - fill options - select image screen (user selects the image in pop up window) - image fills shape.

Thanks in advance!

Lee
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
So you want 10 popups asking the use what image they want in each box?
 
Upvote 0
So you want 10 popups asking the use what image they want in each box?
Hi Efan,

I would like one macro for each (i can amend the macro for each rectangles name).

For example 1st macro button applies to rectangle 1 only. 2nd macro button to rectangle 2 only...etc.etc
 
Upvote 0
This will ask user to chose a file when button is clicked
Please make 10 buttons and assign First code
VBA Code:
Sub Btn1_Clicked()
to first button,
VBA Code:
Sub Btn2_Clicked()
to second button but change
VBA Code:
    ChangeAllPictures "Rectangle 1"
to
VBA Code:
    ChangeAllPictures "Rectangle 2"
etc

VBA Code:
Sub Btn1_Clicked() ' Make 10 of these and change the name, no need to duplicate anything else
    ChangeAllPictures "Rectangle 1"
End Sub


Function ChangeAllPictures(SelectedShape As String)

Dim ThisShape As Shape
Dim Img As String

'For Each ThisShape In ActiveSheet.Shapes
    'If ThisShape.Name Like "Rectangle*" Then
        With ActiveSheet.Shapes.Range(SelectedShape).Fill
            .Visible = msoTrue
            Img = SelectImage
            If Img = "Cancel" Then Exit Function
            .UserPicture Img
            .TextureTile = msoFalse
            .RotateWithObject = msoTrue
        End With
    'End If
'Next ThisShape
End Function


Function SelectImage() As String
Dim lr As Long
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .ButtonName = "Submit"
    .Title = "Select an image file"
    .Filters.Clear
    .Filters.Add "All Pictures", "*.emf;*.wmf;*.jpg;*.jpeg;*.jfif;*.jpe;*.png;*.bmp;" _
        & "*.dib;*.rle;*.jif;*.emz;*.wmz;*.tif;*.tiff;*.svg;*.ico"
    
    If .Show = -1 Then
        Dim Img As Object
        SelectImage = .SelectedItems(1)
    Else
        MsgBox ("Cancelled.")
        SelectImage = "Cancel"
    End If
End With
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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