How do I convert a picture's .Width to .Column.Width so I can embed it into a cell whose .ColumnWidth must be set?

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
I did research this but cannot figure it out.

I am trying to embed a picture into a cell. My problem is that I need to convert the picture's .Width to the .ColumnWidth for the cell into which the picture will be embedded, so the cell is wide enough. How do I do the conversion?

I have researched this but the best I could find is the following sub that converts a CELL's .Width to .ColumnWidth.

Source: What is the unit of Excel column width? - Simple Excel VBA

VBA Code:
Sub f_width(rng As Range, i As Long)

Dim j As Long

With rng
    For j = 1 To 3
        .ColumnWidth = i / .Width * .ColumnWidth
    Next j
End With

End Sub

The logic works well to convert a CELL's .Width to .ColumnWidth. But I need to convert the PICTURE's .Width to .ColumnWidth for the cell into which the picture is embedded.

I hope that a math plus Excel wizard can tell me how to do this.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Where does the picture come from?
From a file or is it in a sheet?
Your question is to convert a picture width to a column width but by the looks of your code snippet, you want to set the column width to picture width. Or did I read it wrong?

Is this of any help? I am sure you can change it to fit your needs but if not, make sure to come back here and let us know what needs to be done.
Code:
Sub Insert_Picture()
    Dim Pict As Variant
    Dim ImgFileFormat As String
    Dim PictCell As Range
    Dim sShape As Picture
    ActiveSheet.Protect False, False, False, False, False
    ImgFileFormat = "All Picture Files(*.emf;*.wmf;*.jpg;*.jpeg;*.jfif;*.jpe;*.png;*.bpm;*.gif;*.gfa;*.emz;*.wmz;*.pcz;*.tif;*.tiff;*.cgm;*.eps;*.pct;*.pict;*.wpg;*.pcd;*.pcx;*.cdr;*.fpx;*.mix), *.bmp"
    Pict = Application.GetOpenFilename(ImgFileFormat)
    Set PictCell = Cells(10, 4)
    Set sShape = ActiveSheet.Pictures.Insert(Pict)
    With sShape.ShapeRange
         .LockAspectRatio = msoFalse
         .Top = PictCell.Top
         .Left = PictCell.Left
         .Width = Columns("D:H").Width
         .Height = Rows("10:30").Height
         .Name = "Picture 1"
     End With
End Sub
 
Last edited:
Upvote 0
The picture is in a worksheet. I want to copy it to a cell but I need to size the cell first. I know PICTURE width but cannot figure out how to size the CELL width so the picture fits into it. I need to convert PICTURE width to the width needed for the CELL.
 
Upvote 0
Are "I know PICTURE width but cannot figure out how to size the CELL width" and "I need to convert PICTURE width to the width needed for the CELL." two different things?
 
Upvote 0
My picture's width is 110.436. I want to place it into a cell. How wide (.ColumnWidth) should the column be for the cell into which the picture is pasted so that the column is wide enough to accommodate the embedded picture?
 
Upvote 0
So far the nearest I've been able to google find is this.
Code:
Sheets("Sheet1").Columns(5).ColumnWidth = ActiveSheet.Shapes("Picture 1").Width / 5.29
 
Upvote 0
Upvote 0
Thanks for the inspiration. I experimented and found that dividing picture width by 5.369 gives pretty precise results.
 
Upvote 0
Yes, I made myself a note to start at 3.5 after experimenting a little.
Mind you, I've always gone the other way, set the picture width relative to the column width.

Good luck Jim
 
Upvote 0
But what if the cell is too small? So you found that there are 3.5 picture .Width units per Excel .ColumnWidth units? The answer you provided -- 5.29 -- seems more correct? I experimented a fair amount to get to the 5.369.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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