How to centre image in merged cells

craigy111

New Member
Joined
Jul 5, 2017
Messages
20
Hello all.
I require a little guidance on how to centre an image within 2 merged cells the code I have been using to centre an image in one cell does not work for 2 cells. No matter how I try I just cant get it to work.
Is there a way to alter the code to allow for this. See bellow for the code I am using:

Code:
 Public Sub FitImagetoCell()'Insert Picture to Cell and Centre
On Error GoTo NOT_SHAPE


With Selection
            .Left = .TopLeftCell.Left + (.TopLeftCell.Width - .Width) / 2
            .Top = .TopLeftCell.Top + (.TopLeftCell.Height - .Height) / 2
        End With
   Exit Sub
   
NOT_SHAPE:
MsgBox "Select a picture."
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If the same Width

Code:
With ActiveSheet.Shapes("Picture 1")
.Top = Range("A1").Top + (Range("A1").Height - .Height) / 2
.Left = Range("A1").Left + Range("A1").Width - (.Width / 2)
End With
 
Last edited:
Upvote 0
Code:
Public Sub CenterInCell()
  Dim r As Range
  
  On Error GoTo Oops

  With Selection
    Set r = .TopLeftCell.MergeArea
    .Left = r.Left + (r.Width - .Width) / 2
    .Top = r.Top + (r.Height - .Height) / 2
  End With
  Exit Sub

Oops:
  MsgBox "Select a picture."
End Sub
 
Upvote 0
Thank you for the code it works perfect.
:cool:
Code:
Public Sub CenterInCell()
  Dim r As Range
  
  On Error GoTo Oops

  With Selection
    Set r = .TopLeftCell.MergeArea
    .Left = r.Left + (r.Width - .Width) / 2
    .Top = r.Top + (r.Height - .Height) / 2
  End With
  Exit Sub

Oops:
  MsgBox "Select a picture."
End Sub
[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,196,288
Messages
6,014,492
Members
441,823
Latest member
GregG26

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