VBA Error That I Cannot Solve

Jingles3X

New Member
Joined
Oct 20, 2021
Messages
34
Office Version
  1. 2013
Platform
  1. Windows
I am new to vba, and have been somewhat successful in some of the basics, but I am having trouble
getting the below code to work. I am trying to select a picture from file, then insert it in a range,
completely centered. I really have tried to make it work, and have tried the search function, but it
just isnt happening for me.

I would be very grateful if someone with knowledge could look at it and tell me where i zigged instead of zagged.

Thanks.
VBA Code:
Private Sub CommandButton11_Click()

Dim fNameAndPath As Variant
Dim img As Picture
Set ws = Worksheets("Cover_Page")


fNameAndPath = Application.GetOpenFilename(Title:="Select Picture To Be Imported")
If fNameAndPath = False Then Exit Sub
    Set img = Worksheets("Cover_Page").Pictures.Insert(fNameAndPath)
    With img
       'Resize Picture to fit in the range....
       .Left = ActiveSheet.Range("B15").Left
       .Top = ActiveSheet.Range("B15").Top
       .Width = ws.("B15:I15").Width
       .Height = ws.("B15:I33").Height
img.Top = cellLocation.Top + (cellLocation.Height / 2) - (myImage.Height / 2)
img.Left = cellLocation.Left + (cellLocation.Width / 2) - (myImage.Width / 2)


       .Placement = 1
       .PrintObject = True
    End With

End Sub
 
That's great, glad I could help.

And good luck. ?☺️

Cheers!
One more quick question.... Within this code, if I wanted to shift the image just a smidgeon to the right or left, is there a single line of code I can add?
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
To nudge it to the right, use the IncrementLeft method . . .

VBA Code:
img.ShapeRange.IncrementLeft 0.75

To nudge it to the left, simply use a negative value. And, of course, you can adjust the value as desired in either case.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,212
Members
448,874
Latest member
b1step2far

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