Forum Discussion
brady
May 01, 2018Copper Contributor
VBA help for a Macro (inserting picture from file)
Hi, I am new to VBA and macros and I was hoping that someone could help me writing the VBA to insert a photo from your personal files. I created the button, but I just need to write the VBA to pu...
kadirguler
Jan 24, 2021Brass Contributor
Hi,
First of all I recommend that the pictures are in the same folder as the workbook.
Next you need to enter some codes in the Worksheet_Change procedure of the worksheet. For example, we can enter the following codes to add the image that with the same name as the value of cell in column A to the cell in column :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pic As Picture
If Intersect(Target, [A:A]) Is Nothing Then Exit Sub
On Error GoTo son
For Each pic In ActiveSheet.Pictures
If Not Application.Intersect(pic.TopLeftCell, Range(Target.Offset(0, 3).Address)) Is Nothing Then
pic.Delete
End If
Next pic
ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\" & Target.Value & ".jpg").Select
Selection.Top = Target.Offset(0, 2).Top
Selection.Left = Target.Offset(0, 3).Left
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = Target.Offset(0, 2).Height
Selection.ShapeRange.Width = Target.Offset(0, 3).Width
son:
End Sub
The picture is sized according to the cell that it is added to.
- DarkmanApr 17, 2021Copper ContributorDear Sirs I have been trying without success to create a VBA or macro to automatically take a folder address of pictures and then match it ascending down from the pic name i.e hf-001.jpg by using the cell ref within the other cell say i.e hf-001 descending down to last populated cell is this possible as all I have managed to do is get 1 picture to populate 1 cell sorry if a big ask as a beginner in all this but trying my best to do as much on my own for a small charity.