insert picture in worksheet

bastian18

New Member
Joined
Aug 15, 2018
Messages
22
Hello.
I am trying to run a code that after reading the value of cell "A:2" must display a picture in the active worksheet.


This is my code:
Code:
Public Sub insertpicture()
If Intersect(Target, Range("A2")) Is Nothing Then
Exit Sub
End If
Dim myPict As Picture
With Range("A20:J20")
'Set myPict = Range("A20:J20").Parent.Pictures.InsertCurrentProject.Path\BDR\Pictures\ & Target.Value & ".jpg")
myPict.Top = .Top
myPict.Width = .Width
myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With
End Sub

However, by the moment I am facing to problems:confused: when running the code :
1. the code vba displays "Error 424 Object Required" for line: "If Intersect(Target, Range("A2")) Is Nothing Then"
2. VBA coloured in red my relative path in line:Set myPict = Range("AI7:J17").Parent.Pictures.InsertCurrentProject.Path\BDR\Pictures\ & Target.Value & ".jpg")


So, any help would be greatly appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
First, "Target" does not seem to be defined.
The Intersect function is expecting a range object named "Target"


This is common in the Workbook_SheetChange event code

I got the following code to work.... changing the PicPath


Code:
Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Intersect(Target, Range("A2")) Is Nothing Then
 Exit Sub
End If


Dim MyPict As Picture
Dim rng As Range
Dim PicPath As String


PicPath = "C:\Users\me\Pictures\" & Target.Value & ".jpg"

If Dir(PicPath) = "" then
 msgbox "Picture file not found!"
 exit Sub
End If

Set MyPict = ActiveSheet.Pictures.Insert(PicPath)

Set rng = Range("a17:c25")
With rng
    MyPict.Top = .Top
    MyPict.Width = .Width
    MyPict.Height = .Height
    MyPict.Left = .Left
End With

End Sub
 
Upvote 0
PatObroen198.
I really appreciate your reply.
When I copy and paste the code VBA displays in red the following line of code
Code:
Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
Upvote 0
PatObroen198.
I really appreciate your reply.
When I copy and paste the code VBA displays in red the following line of code
Code:
Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

sorry.... there's an extra "Sub"
just copy the code between Sub ... and End Sub into the workbook_SheetChange event.
s
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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