VBA Code - Center/Size picture across merged cells

Spaztic

New Member
Joined
Jul 27, 2023
Messages
30
Office Version
  1. 365
Platform
  1. Windows
I have a code that works great for a cell that isn't merged. The idea is to size and center the picture in whatever cell I select. (bottom image)

However, I want this to work when I've merged several columns too. The code is still just recognizing column B for inserting the picture and it looks like this:
1707083874681.png

But I'd like it to look like this:
1707083923241.png


VBA Code:
Sub InsertPic()

'Add picture...centered...scaled
    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Picture
  
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
      
    If fNameAndPath = False Then Exit Sub
  
    Set rng = ActiveCell
  
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.7
        Else
            .Height = rng.Height * 0.7
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
  
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try changing this line :
VBA Code:
Set rng = ActiveCell
to this :
VBA Code:
Set rng = ActiveWindow.RangeSelection
 
Upvote 0
Solution

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

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