Paste Image and resize cell with VBA

Caribeiro77

Well-known Member
Joined
Sep 24, 2010
Messages
1,261
Hey guys, how you all doing...
I have a question.
How do i paste an image ( for example in cell A1) and automaticly resize cell to fit image
Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Is the "image" already copied to the clipboard or will it come from a file or shape or other?
 
Upvote 0
Sorry, my Hughes internet is not working well today.

I normally fit the shape to the cell. Resizing the columnwidth to that shape is being a problem. See the comment for some conversion factors that I tried. It may be a problem with ratios as well.

I set it up so you can look at pic height and width as well.

Code:
Sub InsertPic()
  Dim picPath$, s As Shape, c As Range, w As Single, h As Single
  Dim pic As StdPicture
  
  Set c = [A1]
  picPath = "C:\Users\lenovo1\Dropbox\Excel\Controls\Picture\New\Picture 1.jpg"
  picPath = "C:\Users\lenovo1\Dropbox\Excel\Controls\Picture\New\Picture 2.jpg"
  
  Set pic = LoadPicture(picPath)
   
  Set s = ActiveSheet.Shapes.AddPicture _
    (picPath, msoFalse, msoTrue, _
    c.Left, c.Top, -1, -1) 'embed files
    Debug.Print pic.Width, s.Width
  'c.ColumnWidth = s.Width / ActiveWindow.PointsToScreenPixelsX(1) / 25.4 * 96 / 72
  c.ColumnWidth = s.Width
  c.RowHeight = s.Height
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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