Aligning a picture within a cell

In Excel 2003, is there a way to vertically & horizontally align a picture within a cell?  it's a JPEG inserted from a file, not a drawn object.  I don't see a way to do this from within "Format Picture", or within "Format/Cells/Alignment", and upgrading to a newer version of Excel is not currently an option...

Thanx...

Answer
Answer

Here is some code that will insert and scale a picture to fit a selected range: select the range prior to running the code (though, of course, any of this can be changed to match what you want)

Sub InsertPicture()

Dim myR As Range
Set myR = Selection
'Insert the picture
ActiveSheet.Pictures.Insert( _
   Application.GetOpenFilename( _
   "JPG picture files (*.jpg),*.jpg", , "Select the picture")).Select
  'scale the picture to the width of the column
myScale = Application.Min(myR.Width / Selection.ShapeRange.Width, _
         myR.Height / Selection.ShapeRange.Height)
Selection.ShapeRange.ScaleWidth myScale, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight myScale, msoFalse, msoScaleFromTopLeft

End Sub

 

 

And here is a sub to insert and center a picture on a specific cell:

Sub InsertCenterPicture()

Dim myR As Range
Set myR = Selection
'Insert the picture
ActiveSheet.Pictures.Insert( _
   Application.GetOpenFilename( _
   "JPG picture files (*.jpg),*.jpg", , "Select the picture")).Select
  'center the picture on the current range selection
Selection.ShapeRange.IncrementLeft (myR.Width - Selection.ShapeRange.Width) / 2

End Sub

 

 

And here is a macro to center an existing picture on a specific cell. Select the picture before running it:

Sub CenterSelectedPicture()

Dim myR As Range
Set myR = Application.InputBox( _
    "Center the selected picture on what cell?", , , , , , , 8)
Selection.ShapeRange.Top = myR.Top
Selection.ShapeRange.Left = myR.Left

Selection.ShapeRange.IncrementLeft (myR.Width - Selection.ShapeRange.Width) / 2

End Sub


 

And here is one that moves an existing picture:

Sub CenterExistingPicture()

Dim myR As Range
ActiveSheet.Shapes("PictureName").Select

Set myR = Application.InputBox( _
    "Center the picture on what cell?", , , , , , , 8)
Selection.ShapeRange.Top = myR.Top
Selection.ShapeRange.Left = myR.Left

Selection.ShapeRange.IncrementLeft (myR.Width - Selection.ShapeRange.Width) / 2

End Sub


HTH,

Bernie

2 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated August 2, 2023 Views 28,801 Applies to: