Inserting picture in cell

Zahhhaaaa

Board Regular
Joined
Jun 29, 2011
Messages
62
Hello again,

I'm using code

Code:
Dim filters As String
Dim filename As Variant    
' Define the file selection filters.    
filters = "Image Files,*.bmp;*.tif;*.jpg;*.png,PNG (*.png),*.png,TIFF (*.tif),*.tif,JPG (*.jpg),*.jpg,All Files (*.*),*.*"    
' Get the file name.    
filename = Application.GetOpenFilename( _        
filters, 0, "Select Image", "Take It", False)    
If filename = False Then Exit Sub    
' Insert the picture.    
InsertPicture CStr(filename), Application.Selection
End Sub
' Insert a picture into a cell.
Sub InsertPicture(filename As String, location As Range)
Dim pic As Picture    
Set pic = ActiveSheet.Pictures.Insert(filename)   
pic.Top = location.Top    
pic.Left = location.Left

to add picture in excel, how do I have to change this code that picture appears on range A51:D66??
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Instead of your code, try this which works for me based on the result you want:

Code:
Sub InsertPicture2()
Dim myPicture As Variant
myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = False Then Exit Sub
Application.ScreenUpdating = False
Dim iLeft#, iTop#, iWidth#, iHeight#
With Range("A51")
iLeft = .Left
iTop = .Top
.Select
End With
Set myPicture = ActiveSheet.Pictures.Insert(myPicture)
With Range("A51:D66")
iWidth = .Width: iHeight = .Height
End With
With myPicture
.Width = iWidth: .Height = iHeight
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hmmm... it works but it's adding every picture at range a3:d18, I don't know why it does that

Here's code I tried to use;

Code:
Private Sub CommandButton4_Click()
Dim filters As String
Dim filename As Variant
' Define the file selection filters.
filters = "Image Files,*.bmp;*.tif;*.jpg;*.png,PNG (*.png),*.png,TIFF (*.tif),*.tif,JPG (*.jpg),*.jpg,All Files (*.*),*.*"
' Get the file name.
filename = Application.GetOpenFilename( _
filters, 0, "Select Image", "Take It", False)
If filename = False Then Exit Sub
' Insert the picture.
InsertPicture CStr(filename), Application.Selection
End Sub
' Insert a picture into a cell.
Sub InsertPicture(filename As String, location As Range)
Dim pic As Picture
With ActiveSheet.Range("A51:D66")
Set pic = ActiveSheet.Pictures.Insert(filename)
pic.Top = location.Top
pic.Left = location.Left
pic.Placement = xlMoveAndSize
End With
End Sub

This works too but it doesn't add image into wanted cell-range, but somewhere a49 and so on.. I'm so close now
 
Last edited:
Upvote 0
If you run my macro as is, it should do for you what it does for me, which is to place a picture on your worksheet and snap it into range A51:D66.
 
Upvote 0
I did, but it still adds picture at range a3:d18, I even tried it on a blank sheet and it does same thing there.
 
Upvote 0
I cannot reproduce the behavior you are describing. I just tested my InsertPicture2 macro again with a picture from another folder and had the same successful result, putting a picture into range A51:D66.
 
Upvote 0
Hmmm...

Thing is, I'm creating somesort of diary, I have code attached to commandbutton that's hiding/unhiding rows 28:66. I have placed commandbutton "add picture" at cell A49 which appears everytime I unhide rows. I tried to use your code and but it placed image at range A3:d18

could this cause the problem?
 
Upvote 0
Without you having posted such other code I could not begin to guess. If you experiment with whatever you are doing, and again, just mine without any intervention or association with your code, you will eventually uncover the answer.
 
Upvote 0
Ok.. hmmm.. Are there any similar codes like previous you gave? User can select item and them add it at specific range
 
Upvote 0
Not from me, it works so it's the only one I use. But you might find other examples with other syntaxes on this or other boards. We've come full circle in that respect with this topic, sorry I cannot assist further. Good luck.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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