VBA to load pictures from a folder

IoanZ

Board Regular
Joined
Mar 2, 2018
Messages
51
Hello

In folder "C:\Pictures" from my pc I Have 3 pictures (first pictures it's called "car" // 2nd pictures "dog" // 3rd pictures "horse") all in format .jpg

In an active workbook in sheet1 I have in Cell A1 inserted word car
A2 inserted word dog
A3 inserted word horse

I want to know which is the vba code to load specific image from path "C:\Pictures" and put them in cells B1 // B2 // B3 at the same size for all
(*A1=car ... in B1 insert picture car..)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
now i use this code but with this code I can load a single image


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)


Dim myPict As Picture
Dim PictureLoc As String


If Target.Address = Range("A2").Address Then


ActiveSheet.Pictures.Delete


PictureLoc = "C:\\Pictures" & Range("A2").Value & ".jpg"


With Range("B2")
Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
.RowHeight = myPict.Height
myPict.Top = .Top
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With


End If


End Sub
 
Upvote 0
I find it

Public Function PictureLookup(Value As String, Location As Range, Index As Integer)


Application.Volatile


Dim lookupPicture As Shape
Dim sheetName As String
Dim picTop As Double
Dim picLeft As Double


sheetName = Location.Parent.Name


'Delete current picture with the same Index if exists
For Each lookupPicture In Sheets(sheetName).Shapes
If lookupPicture.Name = "PictureLookup" & Index Then
lookupPicture.Delete
End If
Next lookupPicture


'Get position of cell calling the UDF
picTop = Location.Top + 10
picLeft = Location.Left + 30



'Add the picture in the right location
Set lookupPicture = Sheets(sheetName).Shapes.AddPicture _
("C:\Pictures" & Value & ".jpg", msoFalse, msoTrue, picLeft, picTop, -1, -1)
lookupPicture.Width = 60
lookupPicture.Height = 60
'change the picture name
lookupPicture.Name = "PictureLookup" & Index


PictureLookup = ""


End Function


=PictureLookup(A8;B8;i)


i=1 to ...1000
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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