Using VBA to insert a picture into a cell

mrtim2232

New Member
Joined
Aug 24, 2017
Messages
48
Morning All,

I have been working on a program that will insert a picture into a cell but cant seem to get it working. Ideally it would insert the picture into the cell thats merged and size the picture to fit the cell. However because the name of the image I want could vary although the folder directory stays the same I need to use a cell value within the code and I think thats the bit the program gets stuck on the code is below and help would be appreciated.

Sub Macro1()
'
' Macro1 Macro
'


'
ActiveSheet.Pictures.Insert("C:\examplefile\test"& Range.("T3").Value & ".png").Select
End Sub
 

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
There is an unneccessary character in your code

Instead of..
Rich (BB code):
ActiveSheet.Pictures.Insert("C:\examplefile\test"& Range.("T3").Value & ".png").Select

Try..
Code:
ActiveSheet.Pictures.Insert("C:\examplefile\test" & Range("T3").Value & ".png").Select
 
Last edited by a moderator:
Upvote 0
Thankyou!

It works great for getting the picture do you have any idea how I would get it to resize the image to fit the merged cell?
 
Upvote 0
Code below
- inserts a picture ("C:\examplefile\test" & Range("T3").Value & ".png") in the active cell
- picture is then resized to fit the cell (or nmerged cell)

Code:
Sub InsertPic()
    Dim PicPath As String, Pic As Picture, ImageCell As Range

    PicPath = "C:\examplefile\test" & Range("T3").Value & ".png"
    Set ImageCell = ActiveCell.MergeArea

    Set Pic = ActiveSheet.Pictures.Insert(PicPath)
    With Pic
        .ShapeRange.LockAspectRatio = msoFalse
        .Left = ImageCell.Left
        .Top = ImageCell.Top
        .Width = ImageCell.Width
        .Height = ImageCell.Height
    End With
End Sub
 
Upvote 0
I am trying to do nearly the exact same thing but without selecting the name of the file in the PicPath. Can this code open the folder the picture is stored in instead for an operator to select?

Thanks
 
Upvote 0
@dlo1503
Rather than hijacking this thread, please start a new thread of your own
In the first post include a copy of the code you are trying to modify and the explanation of what you want it to do
PM me a link to that thread and I will look at it when I get some time
thanks
 
Upvote 0
Code below
- inserts a picture ("C:\examplefile\test" & Range("T3").Value & ".png") in the active cell
- picture is then resized to fit the cell (or nmerged cell)

Code:
Sub InsertPic()
    Dim PicPath As String, Pic As Picture, ImageCell As Range

    PicPath = "C:\examplefile\test" & Range("T3").Value & ".png"
    Set ImageCell = ActiveCell.MergeArea

    Set Pic = ActiveSheet.Pictures.Insert(PicPath)
    With Pic
        .ShapeRange.LockAspectRatio = msoFalse
        .Left = ImageCell.Left
        .Top = ImageCell.Top
        .Width = ImageCell.Width
        .Height = ImageCell.Height
    End With
End Sub
Thank you for the guidance. It has helped me a lot in understanding on how to insert images and alter their display according to situational needs.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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