Position of Inserted Picture in VBA Code not working

mexivano

New Member
Joined
Aug 11, 2015
Messages
4
Hello,

I am trying to insert a picture such that it spans the width and high of range G4:G8, which is about 1.13 in by 1.2 in according to the format graphic size & properties.

Unfortunately, my picture continues to be inserted in range E4:E8 and *I DONT KNOW WHY* :mad::mad::mad::mad:

This is very frustrating and any help would be extremely appreciated - code below:

Code:
Sub GetPic()


' This macro is designed to allow users to click the NPC picture and automatically add their own from a file on their explorer
'
' Variables
'
Dim fNameAndPath As Variant
Dim img As Picture
Dim rng As Range


Set rng = ActiveSheet.Range("G4:G8")
fNameAndPath = Application.GetOpenFilename(Title:="Select Picture to be imported") ' Opening windows browser to find picture


On Error GoTo errHandler


If fNameAndPath = False Then MsgBox ("Error importing picture, please try again.")


Set img = ActiveSheet.Pictures.Insert(fNameAndPath)


' Resizing image to fit inside the area


With img


    .Left = rng.Left
    .Top = rng.Top
    .Width = rng.Width
    .Height = rng.Height
    .Placement = 1
    .PrintObject = True
    
End With


exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not insert picture"
    Resume exitHandler
    
End Sub
 
Last edited by a moderator:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hello,

I am trying to insert a picture such that it spans the width and high of range G4:G8, which is about 1.13 in by 1.2 in according to the format graphic size & properties.

Unfortunately, my picture continues to be inserted in range E4:E8 and *I DONT KNOW WHY* :mad::mad::mad::mad:

This is very frustrating and any help would be extremely appreciated - code below:

Code:
Sub GetPic()
    ' This macro is designed to allow users to click the NPC picture and automatically add their own from a file on their explorer
    ' Variables
    Dim fNameAndPath As Variant
    Dim img As Picture
    Dim rng As Range
    
    Set rng = ActiveSheet.Range("G4:G8")
    fNameAndPath = Application.GetOpenFilename(Title:="Select Picture to be imported") ' Opening windows browser to find picture
    
    'On Error GoTo errHandler
    
    If fNameAndPath = False Then MsgBox ("Error importing picture, please try again.")
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    ' Resizing image to fit inside the area
    With img
[COLOR=#ff0000]        .ShapeRange.LockAspectRatio = msoFalse[/COLOR]
        .Left = rng.Left
        .Top = rng.Top
        .Width = rng.Width
        .Height = rng.Height
        .Placement = 1
        .PrintObject = True
    End With
    
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not insert picture"
    Resume exitHandler


End Sub


Add that line


Do you have hidden columns or rows?
Do you have cells merged?
 
Upvote 0
Add that line


Do you have hidden columns or rows?
Do you have cells merged?

Hi Dante,

Yes, the workboook (not created by me) essentially spans all columns and rows 1:32. The visible columns are columns A:H and the hidden range then is, I:XFE, followed by the last column displayed XFD.

I would like to place the picture on this specific sheet on range G4:H8 for the entire width of the G column itself. In other words, put the picture on column G, with its width sized by the exisiting width of G and length of rows 4 through 8.

Ivan
 
Upvote 0
Hi Dante,

Yes, the workboook (not created by me) essentially spans all columns and rows 1:32. The visible columns are columns A:H and the hidden range then is, I:XFE, followed by the last column displayed XFD.

I would like to place the picture on this specific sheet on range G4:H8 for the entire width of the G column itself. In other words, put the picture on column G, with its width sized by the exisiting width of G and length of rows 4 through 8.

Ivan

Also, when I add that line, the picture gets inserted on E4:E8 which is exactly what has been happening :(
 
Upvote 0
Also, when I add that line, the picture gets inserted on E4:E8 which is exactly what has been happening :(

You could upload a copy of your file to a free site such www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
You could upload a copy of your file to a free site such www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.

Hi Dante! Thanks for all yoour help! It seems to be working now actually, so I really apprecaite the help!

Ivan
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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