Macro for Inserting Picture to Cell

WingSystems

New Member
Joined
Aug 24, 2016
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hey all,

I've been looking high and low on this site and others for help with a Macro I am trying to write and looking for some tips.

I am looking to do these things:
1. Click CommandButton
2. Pull up the insert picture dialogue
3. Have picture selected inserted to specific cell

It seems there are several solutions for inserting pictures when specifying a path...but I'm unable to find any that allow you to pull up the insert picture dialogue/file explorer and then let you input to a specific cell.

Any help is greatly appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Re: Help with Macro for Inserting Picture to Cell

Hi try this code given by RickXL given in one of the posts here.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim strFileSelected  As String
    'Insert picture in column A
    If Target.Count = 1 And Target.Column = 1 Then
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "JPEG File", "*.jpg"
            .Filters.Add "PNG File", "*.png"
            .Title = "Select Image"
            .Show
            If .SelectedItems.Count Then
                strFileSelected = .SelectedItems(1)
            Else
                MsgBox "Cancelled by user!"
                Exit Sub
            End If
        End With
        With Me.Pictures.Insert(strFileSelected)
            .ShapeRange.LockAspectRatio = False
            .Top = Target.Top
            .Left = Target.Left
            .Width = Target.Width
            .Height = Target.Height
            .Placement = xlMoveAndSize
        End With
    End If
End Sub
 
Upvote 0
Re: Help with Macro for Inserting Picture to Cell

A similar approach, but without the Column A restriction...

Code:
Sub InsertPicture1()

Dim fName As String
Dim pic As Picture
Dim r As Range

fName = Application.GetOpenFilename( _
    FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
    Title:="Please select an image...")
If fName = "False" Then Exit Sub

Set r = ActiveCell 'This line sets the ActiveCell as the Target for insertion of the picture
Set pic = Worksheets("Sheet1").Pictures.Insert(fName)

With pic
    .ShapeRange.LockAspectRatio = msoTrue 'This line locks the ratio of Width to Height if =msoTrue, no distortion
    .Left = r.Left 'The  ' .Left=' and ' .Top=' in this and the next line set the respective locations for the placement of the picture
    .Top = r.Top
    '.Width = r.Width 'You can put an single quote before this line if you want to hold just the Height of the picture to be the height of the cell where it will be placed
    .Height = r.Height            'You can put an single quote before this line if you want to hold just the Width of the picture to be the width of the cell where it will be placed
End With

End Sub
Cheers,

tonyyy
 
Upvote 0
Re: Help with Macro for Inserting Picture to Cell

Many thanks! I was able to modify this code to my needs by changing the ActiveCell to the specific cell.

A similar approach, but without the Column A restriction...

Code:
Sub InsertPicture1()

Dim fName As String
Dim pic As Picture
Dim r As Range

fName = Application.GetOpenFilename( _
    FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
    Title:="Please select an image...")
If fName = "False" Then Exit Sub

Set r = ActiveCell 'This line sets the ActiveCell as the Target for insertion of the picture
Set pic = Worksheets("Sheet1").Pictures.Insert(fName)

With pic
    .ShapeRange.LockAspectRatio = msoTrue 'This line locks the ratio of Width to Height if =msoTrue, no distortion
    .Left = r.Left 'The  ' .Left=' and ' .Top=' in this and the next line set the respective locations for the placement of the picture
    .Top = r.Top
    '.Width = r.Width 'You can put an single quote before this line if you want to hold just the Height of the picture to be the height of the cell where it will be placed
    .Height = r.Height            'You can put an single quote before this line if you want to hold just the Width of the picture to be the width of the cell where it will be placed
End With

End Sub
Cheers,

tonyyy
 
Upvote 0
Re: Help with Macro for Inserting Picture to Cell

You're welcome. Glad that worked out for you.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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