"Picture 1" change name problem

jcyong

New Member
Joined
Sep 6, 2014
Messages
4
Hi All,
I have a problem with renaming and calling "Picture 1". I'm not a pro so bare with me...
I have the following code which calls a image from harddisk. After the image is loaded in a worksheet Excel set this name of the image to "Picture 1" This "Picture 1" is then copied and paste through VBA in the worksheet.

So suppose I want the name to be "IMAGEKLM" instead of "Picture 1"? How do I that?

I have the following code. Load image "2.jpg" from C drive and set the size and position:
Sub TestInsertPictureInRange()
InsertPictureInRange "C:\1\2.jpg", _
Range("A1:D2")
End Sub​
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)​
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double

If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)​
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With​
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing

End Sub​

Ok so far so good. Now I want to copy and paste this "Picture1" in the same worksheet with this line of code.
Sub pasteImage()
ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.Copy
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 412.5
Selection.ShapeRange.IncrementTop -12

Selection.ShapeRange.Height = 170.0787401575
Selection.ShapeRange.Width = 170.0787401575

Selection.ShapeRange.Rotation = 90
End Sub​

So the question is how can give this image"2.jpg" a name like "IMAGEKLM" so that the line of code would be
ActiveSheet.Shapes.Range(Array("IMAGEKLM")).Select ??​


Any help would be appreciated, and thank you in advance...

Jim
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

jcyong

New Member
Joined
Sep 6, 2014
Messages
4
.
.

ActiveSheet.Shapes("Picture 1").Name = "IMAGEKLM"



Hi gpeacock, thanks for the quick reply....and work like a charm, and I understand!....so to continou the next thing is as follows...

When the user loaded a image, Excel makes this Picture1 (I change to IMAGEKLM) but...when the user delete this image in the worksheet and again run the script Excel name the new image Picture 2 , correct? Ofcourse the line "ActiveSheet.Shapes("Picture 1").Name = "IMAGEKLM"" doesn't work, or I have to change Picture1 to Picture 2.

My Question is : how can I make it so that the script automaticly change any "Picture x" to IMAGEKLM ? So for instance when the user run VBA 13th time the script image would be "Picture 13" again I need to change this to IMAGEKLM without doing this by hand.....Hope you understand what I mean....

Thanks for replyiing


Jim
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "IMAGEKLM"
 

jcyong

New Member
Joined
Sep 6, 2014
Messages
4

ADVERTISEMENT

Try

Code:
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "IMAGEKLM"


YESSSS! , thanks guys all is working like a charm......thanks again

Cheers

Jim
 

ParamRay

Well-known Member
Joined
Aug 6, 2014
Messages
1,195
.
.

You can also name the picture when you insert it:

Code:
Sub InsertPic()
        
    Dim pic As Picture
    Set pic = ActiveSheet.Pictures.Insert("C:\Users\jsmith\Desktop\my_photo.jpg")
    pic.Name = "MyPhoto"

End Sub
 

jcyong

New Member
Joined
Sep 6, 2014
Messages
4
Sub InsertPic()

Dim pic As Picture
Set pic = ActiveSheet.Pictures.Insert("C:\Users\jsmith\Desktop\my_photo.jpg")
pic.Name = "MyPhoto"

End Sub



Ah yes, I was wondering I'm doing not so long VBA but is it possible to load a image through some kind of form? If positive Can you give me a pointing direction were to look in VBA?

Thanks again, great help....
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,315
Members
414,053
Latest member
Dual Showman

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
Top