Create Macro to Insert and Re-size picture.

Jonny1990

New Member
Joined
Feb 4, 2018
Messages
2
Hi, I would like to create a macro to allow me to easily insert, resize and center a picture in a selected cell, or range of merged cells.

I would like to select a cell, key in the assigned shortcut and then have the "Insert Picture" dialogue box appear. Once I have selected the picture I would like to insert I would then like it to be re-sized to height- 5.05cm and width 7.6cm and centered in the middle of the selected cell.

Any help anyone could give me on this would be greatly appreciated.

Thank you in advance! :)
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The following macro will prompt the user to select a picture, insert it into the active worksheet, and resize it within the active/selected cell...

Code:
Option Explicit

Sub InsertAndCenterPicture()


    Dim vFileName As Variant
    Dim oPicture As Shape
    
    If TypeName(ActiveSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active!", vbExclamation
        Exit Sub
    End If
    
    vFileName = Application.GetOpenFilename( _
        FileFilter:="Pictures, *.gif;*.jpg;*.png", _
        Title:="Select Picture")
        
    If vFileName = False Then Exit Sub
    
    Set oPicture = ActiveSheet.Shapes.AddPicture( _
        Filename:=vFileName, _
        LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, _
        Left:=1, _
        Top:=1, _
        Width:=Application.CentimetersToPoints(5.05), _
        Height:=Application.CentimetersToPoints(7.6))
        
    With oPicture
        .Left = ActiveWindow.RangeSelection.Left + (ActiveWindow.RangeSelection.Width - .Width) / 2
        .Top = ActiveWindow.RangeSelection.Top + (ActiveWindow.RangeSelection.Height - .Height) / 2
    End With
    
End Sub

If you would like to include other types of pictures from which to select, add them to the FileFilter for GetOpenFilename.

Hope this helps!
 
Upvote 0
Thanks Domenic!

I seem to have an issue though, in that when I open the document on another computer the size of the pictures that I have added using the macro change slightly, while the pictures that I add by simply clicking insert, pictures and re-size manually are unaffected.

Have you any idea why this would be?

Thanks again for your help it is very much appreciated! :)
 
Upvote 0
If you right-click the picture and check the actual size, have the width and height actually changed from 5.05 and 7.6, respectively?
 
Upvote 0

Forum statistics

Threads
1,216,165
Messages
6,129,250
Members
449,497
Latest member
The Wamp

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