Combine two VBAs, Auto Resize Image and then Center Align it

Jeremy Zhang

New Member
Joined
Mar 8, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am new to VBA, I have two VBA codes from the web and they work fine for me except below:
  • For the auto resize image, I would like to set the image size a little bit smaller than the cells
  • I woud also like to combine these two codes to one, so that I can complete these two actiosn by one time
Thank you!

VBA Code:
Public Sub AutoResizeImage()
On Error GoTo Select_Image
Dim ZImageWtoHRatio As Single
Dim QWtoHRatio As Single
With Selection
ZImageWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
QWtoHRatio = .Width / .RowHeight
End With
Select Case ZImageWtoHRatio / QWtoHRatio
Case Is > 1
With Selection
.Width = .TopLeftCell.Width
.Height = .Width / ZImageWtoHRatio
End With
Case Else
With Selection
.Height = .TopLeftCell.RowHeight
.Width = .Height * ZImageWtoHRatio
End With
End Select
With Selection
.Top = .TopLeftCell.Top
.Left = .TopLeftCell.Left
End With
Exit Sub
Select_Image:
MsgBox "Choose an Image and Run the Macro."
End Sub

VBA Code:
Sub CenterPicturesHnV()

    Dim myR As Range
    Dim myP As Shape

    For Each myP In ActiveSheet.Shapes
        Set myR = myP.TopLeftCell
        myP.Left = myR.Left + (myR.Width - myP.Width) / 2
        myP.Top = myR.Top + (myR.Height - myP.Height) / 2
     Next myP
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Combined


VBA Code:
Public Sub AutoResizeImage()
On Error GoTo Select_Image
Dim ZImageWtoHRatio As Single
Dim QWtoHRatio As Single
Dim myR As Range
Dim myP As Shape
With Selection
    ZImageWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
    QWtoHRatio = .Width / .RowHeight
End With
Select Case ZImageWtoHRatio / QWtoHRatio
Case Is > 1
    With Selection
    .Width = .TopLeftCell.Width
    .Height = .Width / ZImageWtoHRatio
    End With
Case Else
    With Selection
    .Height = .TopLeftCell.RowHeight
    .Width = .Height * ZImageWtoHRatio
    End With
End Select
With Selection
    .Top = .TopLeftCell.Top
    .Left = .TopLeftCell.Left
End With
' Center Pictures
For Each myP In ActiveSheet.Shapes
    Set myR = myP.TopLeftCell
    myP.Left = myR.Left + (myR.Width - myP.Width) / 2 '## try playing with the /2 maybe 2.25
    myP.Top = myR.Top + (myR.Height - myP.Height) / 2
Next myP
Exit Sub

Select_Image:
    MsgBox "Choose an Image and Run the Macro."
End Sub
 
Upvote 0
Solution
Combined


VBA Code:
Public Sub AutoResizeImage()
On Error GoTo Select_Image
Dim ZImageWtoHRatio As Single
Dim QWtoHRatio As Single
Dim myR As Range
Dim myP As Shape
With Selection
    ZImageWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
    QWtoHRatio = .Width / .RowHeight
End With
Select Case ZImageWtoHRatio / QWtoHRatio
Case Is > 1
    With Selection
    .Width = .TopLeftCell.Width
    .Height = .Width / ZImageWtoHRatio
    End With
Case Else
    With Selection
    .Height = .TopLeftCell.RowHeight
    .Width = .Height * ZImageWtoHRatio
    End With
End Select
With Selection
    .Top = .TopLeftCell.Top
    .Left = .TopLeftCell.Left
End With
' Center Pictures
For Each myP In ActiveSheet.Shapes
    Set myR = myP.TopLeftCell
    myP.Left = myR.Left + (myR.Width - myP.Width) / 2 '## try playing with the /2 maybe 2.25
    myP.Top = myR.Top + (myR.Height - myP.Height) / 2
Next myP
Exit Sub

Select_Image:
    MsgBox "Choose an Image and Run the Macro."
End Sub
Thank you so much! The combined codes work for most images, but sometimes the image may go to another cell during the centerring process, did you see any issues in the code on image properties etc.?
 
Upvote 0
Just noticed I referred to the wrong part for sizing. This affects the size.

VBA Code:
With Selection
    ZImageWtoHRatio = .Width / .Height
End With
 
Upvote 0

Forum statistics

Threads
1,215,607
Messages
6,125,818
Members
449,262
Latest member
hideto94

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