inserting pictures through vba, but the position and range doesn't match

Jbirds

New Member
Joined
Sep 7, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi,
i made a macro by clicking the cell and inserting a picture image. But, the vba range and the sheet range doesn't match.
ex. i want to insert a picture in cell b6 but it seems the picture inserts in between b5 and b6.
this is my code below

Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, cancel As Boolean)


If Not Application.Intersect(target, Range("b6")) Is Nothing Then
cancel = True

Dim a As String
Dim img As Object

a = Application.GetOpenFilename("画像,*.jpg", , "画像ファイルの選択")
If a = "false" Then Exit Sub


Set img = ActiveSheet.Shapes.AddPicture(Filename:=a, linktofile:=False, savewithdocument:=True, Left:=0, Top:=0, Width:=-1, Height:=-1)

With img

'Move and Resize Image
img.Select

Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.Left = ActiveSheet.Range("b6").Left
Selection.Top = ActiveSheet.Range("b6").Top
Selection.Width = ActiveSheet.Range("b6:e6").Width
Selection.Height = ActiveSheet.Range("b6:b16").Height

End With
End If

end sub


it is supposed to be inserting on cell B6.

please help me. thanks

p.s. please ignore the japanese characters
 

Attachments

  • capture.PNG
    capture.PNG
    143.8 KB · Views: 23

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You are setting the width of the picture to the width of B6:E6 and the height to the height of B6:B16, so the picture will be in the range B6:E16.
 
Upvote 0
yes but if you see in the picture i attached, its not exactly inserted in the right range. its is supposed to be in the yellow background.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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