Centre and scale image inserted into merged cells

alexg7828

New Member
Joined
Aug 4, 2017
Messages
22
Hi,

I am using the below macro to insert images into a group of merged cells, it works perfectly however i would also like it to automatically scale and centre the image to fit in the merged cells... Any ideas ?

Thanks in advance Alex

Code:
Sub InsertImageBox1()
Dim FName$
With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = [a67]                    ' path from cell
    .Filters.Clear
    .Filters.Add "JPEGS", "*.jpg; *.jpeg"
    .Filters.Add "GIF", "*.GIF"
    .Filters.Add "Bitmaps", "*.bmp"
    .AllowMultiSelect = 0
    If .Show = True Then
        FName = .SelectedItems(1)
    Else
        MsgBox "Operation Cancelled"
        Exit Sub
    End If
End With
ActiveSheet.Pictures.Insert (FName)
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I have trawled through posts on here and found something that looks similar to what i want to do but not sure how to implement it with the first part of my code, credit to BiocideJ for the below (not sure if i need to reference the post or something else on here...)

Code:
Sub FIT_IMG_TO_RNG()
'Set CellLoc as the cell that you want to fit the image into or any cell within the merged area you wish to place the image
Set CellLoc = Range("A1")
PictureFileName = "[URL="https://www.mrexcel.com/forum/redirect-to/?redirect=http%3A%2F%2Fwww.google.com%2Flogos%2Fdoodles%2F2015%2Fbeethovens-245th-birthday-4687587541254144-hp.jpg"]http://www.google.com/logos/doodles/...1254144-hp.jpg[/URL]"
    Set p = CellLoc.Parent.Pictures.Insert(PictureFileName)
    
    p.Left = CellLoc.MergeArea.Left
    p.Top = CellLoc.MergeArea.Top
    
    If p.Width > CellLoc.MergeArea.Width Then p.Width = CellLoc.MergeArea.Width
    If p.Height > CellLoc.MergeArea.Height Then p.Height = CellLoc.MergeArea.Height
    'Center
        
    If p.Width < CellLoc.MergeArea.Width Then p.Left = CellLoc.MergeArea.Left + (CellLoc.MergeArea.Width - p.Width) / 2
    If p.Height < CellLoc.MergeArea.Height Then p.Top = CellLoc.MergeArea.Top + (CellLoc.MergeArea.Height - p.Height) / 2
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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