Resize picture macro Excel 2007

hank153

New Member
Joined
Aug 30, 2014
Messages
2
I came across the following macro to resize a picture to fit any cell in Excel. It works great as long as it is not a MERGED cell.
Can anyone tell he how to change it to make it work in merged cells or is that possible???

I don't know who wrote the code, but they did a great job.

Public Sub FitPic()
On Error GoTo NOT_SHAPE
Dim PicWtoHRatio As Single
Dim CellWtoHRatio As Single
With Selection
PicWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
CellWtoHRatio = .Width / .RowHeight
End With
Select Case PicWtoHRatio / CellWtoHRatio
Case Is > 1
With Selection
.Width = .TopLeftCell.Width
.Height = .Width / PicWtoHRatio
End With
Case Else
With Selection
.Height = .TopLeftCell.RowHeight
.Width = .Height * PicWtoHRatio
End With
End Select
With Selection
.Top = .TopLeftCell.Top
.Left = .TopLeftCell.Left
End With
Exit Sub
NOT_SHAPE:
MsgBox "Select a picture before running this macro."
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the board.

See if this does what you want:
Code:
Public Sub FitPic()
If TypeName(Selection) <> "Picture" Then GoTo NOT_SHAPE
Dim PicWtoHRatio As Single
Dim CellWtoHRatio As Single
With Selection
    PicWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
    If .MergeCells Then
        CellWtoHRatio = .MergeArea.Width / .MergeArea.Height
    Else
        CellWtoHRatio = .Width / .RowHeight
    End If
End With
Select Case PicWtoHRatio / CellWtoHRatio
    Case Is > 1
        With Selection
            If .TopLeftCell.MergeCells Then
                .Width = .TopLeftCell.MergeArea.Width
            Else
                .Width = .TopLeftCell.Width
            End If
            .Height = .Width / PicWtoHRatio
        End With
    Case Else
        With Selection
            If .TopLeftCell.MergeCells Then
                .Height = .TopLeftCell.MergeArea.Height
            Else
                .Height = .TopLeftCell.RowHeight
            End If
            .Width = .Height * PicWtoHRatio
        End With
End Select
With Selection
    .Top = .TopLeftCell.Top
    .Left = .TopLeftCell.Left
End With
Exit Sub
NOT_SHAPE:
MsgBox "Select a picture before running this macro."
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

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