Excel VBA Macro

llravlin

New Member
Joined
Sep 26, 2012
Messages
15
Hi,

I have this code to be able to select a picture and then select a cell to insert the picture. Everything works until trying to put the picture in the cell. I receive the following error: "Unable to get the Insert property of the Pictures class". Cannot figure out what I am doing wrong...Also, is there a way to resize as well in this code?

Sub Insert_Pict()
Dim Pict
Dim ImgFileFormat As String
Dim PictCell As Range
Dim Ans As Integer


ActiveSheet.Protect True, True, True, True, True
ImgFileFormat = "Image Files (*.bmp),others, tif (*.tif),*.tif, jpg (*.jpg),*.jpg"


GetPict:
Pict = Application.GetOpenFilename(ImgFileFormat)
'Note you can load in any nearly file format
If Pict = False Then End


Ans = MsgBox("Open : " & Pict, vbYesNo, "Insert Picture")
If Ans = vbNo Then GoTo GetPict


'Now paste to userselected cell
GetCell:
Set PictCell = Application.InputBox("Select the cell to insert into", Type:=8)
If PictCell.Count > 1 Then MsgBox "Select ONE cell only": GoTo GetCell
PictCell.Select
ActiveSheet.Pictures.Insert(Pict).Select


End Sub

Thanks,
Laura
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Laura,

If you protect a Worksheet sheet manually you will see in the right-click context menu that 'Insert' is disabled.
The first line of your code is protecting the sheet.
I would imagine you need to Unprotect the sheet do the Insert then protect it?

Not sure about the resize.
 
Upvote 0
ok, I took the protecting out completely. It runs without any errors, but the picture does not go into the cell I selected...why??

Sub Insert_Pict()
Dim Pict
Dim ImgFileFormat As String
Dim PictCell As Range
Dim Ans As Integer




ImgFileFormat = "Image Files (*.jpg),*.jpg,(*.bmp),others, tif (*.tif),*.tif,"




GetPict:
Pict = Application.GetOpenFilename(ImgFileFormat)
'Note you can load in any nearly file format
If Pict = False Then End






'Now paste to userselected cell
GetCell:
Set PictCell = Application.InputBox("Select the cell to insert into", Type:=8)
If PictCell.Count > 1 Then MsgBox "Select ONE cell only": GoTo GetCell
PictCell.Select
ActiveSheet.Pictures.Insert(Pict).Select


End Sub
 
Upvote 0
Try something like this...

Code:
    [COLOR=green]'Now paste to userselected cell[/COLOR]
GetCell:
    [COLOR=darkblue]Set[/COLOR] PictCell = Application.InputBox("Select the cell to insert into", Type:=8)
    [COLOR=darkblue]If[/COLOR] PictCell.Count > 1 [COLOR=darkblue]Then[/COLOR] MsgBox "Select ONE cell only": [COLOR=darkblue]GoTo[/COLOR] GetCell
    PictCell.Select
[COLOR=#ff0000]    With ActiveSheet.Pictures.Insert(Pict)
        .Width = PictCell.Width
        .Height = PictCell.Height
    End With[/COLOR]
 
Upvote 0
Thanks, but that didn't work either. The picture gets inserted into the first cell of the worksheet. This is so frustrating.
 
Upvote 0
It worked for me. It inserted the picture in the cell I selected when prompted then resized it to that cell.

Maybe try this...

Code:
    [COLOR=green]'Now paste to userselected cell[/COLOR]
GetCell:
    [COLOR=darkblue]Set[/COLOR] PictCell = Application.InputBox("Select the cell to insert into", Type:=8)
    [COLOR=darkblue]If[/COLOR] PictCell.Count > 1 [COLOR=darkblue]Then[/COLOR] MsgBox "Select ONE cell only": [COLOR=darkblue]GoTo[/COLOR] GetCell
    PictCell.Select
    [COLOR=darkblue]With[/COLOR] ActiveSheet.Pictures.Insert(Pict)
        .Left = PictCell.Left
        .Top = PictCell.Top
        .Width = PictCell.Width
        .Height = PictCell.Height
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
Last edited:
Upvote 0
I used this, which worked for me.
I should point out that the picture isn't 'In' the cell it is floating on top.
You have to right click and change the properties of the picture to lock it into changing position and size with the cell.

Code:
ActiveSheet.Pictures.Insert(Pict).Select

With Selection
    .Width = ActiveCell.Width
    .Height = ActiveCell.Height
    .Top = ActiveCell.Top
    .Left = ActiveCell.Left
End With
 
Upvote 0
At least you have it working, which is the important thing.
AlphaFrogs code works fine too.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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