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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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,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