1 Attachment(s)
Insert Picture in a Cell UDF
Hi All,
Here is a UDF, which helps to insert a picture into a cell.
Enjoy !!
Code:
Function INSERTPICTURE(ByVal PictureFullName As String, Optional ByVal PicWidth As Single = 200, _
Optional ByVal PicHeight As Single = 150)
'// Author : Kris @ ExcelFox.com
Dim CellActive As Range
Dim picPicture As Object
Set CellActive = Application.Caller
For Each picPicture In CellActive.Parent.Pictures
If picPicture.TopLeftCell.Address = CellActive.Address Then
picPicture.Delete
Exit For
End If
Next
Set picPicture = CellActive.Parent.Pictures.Insert(PictureFullName)
With picPicture
.Left = CellActive.Left + 1
.Top = CellActive.Top + 1
.Width = PicWidth
.Height = PicHeight
End With
End Function
use like =INSERTPICTURE("C:\Pictures\MyPicture.jpg")
PFA sample workbook.
What about referencing a cell with the image location in it?
This is really cool. Is it possible to set it up so that you can use it like =INSERTPICTURE(A2) where cell A2 contains either a hyperlink to the image or the path (images are stored in .\PictureFiles)? I'm trying to get the picture to change along with other data that is displayed. I can get the hyperlink or path to change in A2 but can't get the image itself to display based on that value.
I see that =INSERTPICTURE() is looking for a "string" that gets entered as PictureFullName for the line Set picPicture = CellActive.Parent.Pictures.Insert(PictureFullName) . Is there a way for it to use the displayed contents of a cell as PictureFullName?
Thks. How keep aspect ratio of picture.
Quote:
Originally Posted by
Admin
Hi
Try this version.
Code:
Function INSERTPICTURE(ByVal PictureFullName As String, Optional ByVal PicWidth As Single = 200, _
Optional ByVal PicHeight As Single = 150) As Boolean
'// Author : Kris @ ExcelFox.com
Dim CA As Range
Dim picPicture
Set CA = Application.Caller
If Val(Application.Version) < 12 Then
For Each picPicture In ActiveSheet.Shapes
If picPicture.TopLeftCell.Address = CA.Address Then
If picPicture.Type = msoLinkedPicture Then
picPicture.Delete
Exit For
End If
End If
Next
Set picPicture = ActiveSheet.Shapes.AddPicture(PictureFullName, 1, 0, CA.Left, CA.Top, PicWidth, PicHeight)
GoTo Finish
End If
For Each picPicture In ActiveSheet.Pictures
If picPicture.TopLeftCell.Address = CA.Address Then
picPicture.Delete
Exit For
End If
Next
Set picPicture = ActiveSheet.Pictures.Insert(PictureFullName)
With picPicture
.Left = CA.Left + 1
.Top = CA.Top + 1
.Width = PicWidth
.Height = PicHeight
End With
INSERTPICTURE = True
Exit Function
Finish:
INSERTPICTURE = True
End Function
Note: It works in 2003, not sure on older versions.
Thanks Kris for your kind support. It work fine on 2003. But I have another question, ist possible keep aspct ratio of original picture. I read several posts in web and can't find any fine answer. Thks a lot for your support, again.
Zapamato