Centering Multiple Images in an Excel Column

JacksonTriggs

New Member
Joined
Nov 4, 2021
Messages
20
Office Version
  1. 365
Platform
  1. MacOS
Hi,

Despite a previous similar post on this subject, it was recommended I post a newer thread question because the original is quite old. I have very limited knowledge about VBA/macros.

I have an Excel spreadsheet with about 20 rows and 2 columns. The first column has an image in each row, however, they're not centered in their respective cells. I would like to fix this. I tried one of the VBA codes from that post (quoted below) but it only does a single, active cell instead of all images in the entire column. Also, when I try to repeat that Macro on a separate cell/image, it forces an error (sorry, I lost track of that code).

Screenshot of my spreadsheet:
Screen Shot 2021-11-04 at 4.28.06 PM.png



Hoping someone might be able to help please. Thank in advance! :)


Code:
Code:
Const inDebug As Boolean = False

Sub CenterPictureIfInActiveCell()
   
'If the Top-Left corner of any Picture is located within the Active Cell
'Then center the picture within the Active Cell


    Dim Pic As Picture
   
    For Each Pic In ActiveSheet.Pictures
   
        If inDebug Then MsgBox Pic.Name
   
        If isInBetween(ActiveCell.Left - 1, ActiveCell.Left + ActiveCell.Width, Pic.Left) And _
           isInBetween(ActiveCell.Top - 1, ActiveCell.Top + ActiveCell.Height, Pic.Top) _
           Then
                Pic.Left = ActiveCell.Left + ((ActiveCell.Width - Pic.Width) / 2)
                Pic.Top = ActiveCell.Top + ((ActiveCell.Height - Pic.Height) / 2)
        End If
       
    Next Pic
      
End Sub


Function isInBetween(lowVal As Long, hiVal As Long, targetVal As Long, Optional Inclusive As Boolean = True) As Boolean


'Return TRUE if the targetVal is between the lowVal and hiVal (Inclusive optional)


    isInBetween = False
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I should add that despite this document having only 20-30 rows, most have over 300 so I'd like to use a macro instead of manually modifying image positioning.
 
Upvote 0
The code below is most likely to do what you want.

VBA Code:
Public Sub CenterAllPicturesInCell()
    
    Dim Pic As Picture
    Application.ScreenUpdating = False
    For Each Pic In ActiveSheet.Pictures
        With Pic.TopLeftCell
            If .Width > Pic.Width And .Height > Pic.Height Then
                If isInBetween(.Left - 1, .Left + .Width, Pic.Left) And isInBetween(.Top - 1, .Top + .Height, Pic.Top) Then
                    Pic.Left = .Left + ((.Width - Pic.Width) / 2)
                    Pic.Top = .Top + ((.Height - Pic.Height) / 2)
                End If
            End If
        End With
    Next Pic
    Application.ScreenUpdating = True
End Sub


Public Function isInBetween(lowVal As Long, hiVal As Long, targetVal As Long, Optional Inclusive As Boolean = True) As Boolean
    'Return TRUE if the targetVal is between the lowVal and hiVal (Inclusive optional)
    If Inclusive Then
        Select Case targetVal
        Case Is < lowVal
        Case Is > hiVal
        Case Else
            isInBetween = True
        End Select
    Else
        Select Case targetVal
        Case Is <= lowVal
        Case Is >= hiVal
        Case Else
            isInBetween = True
        End Select
    End If
End Function
 
Upvote 0
Solution
You are welcome and thanks for the feedback.
 
Upvote 0
I don't mean to push it, and I'm sorry for this additional request, but is it possible to made a slight modification to make the images align top-left instead? My bad.
 
Upvote 0
That requires much less code:

VBA Code:
Public Sub JacksonTriggs()
    Dim Pic As Picture
    Application.ScreenUpdating = False
    For Each Pic In ActiveSheet.Pictures
        With Pic.TopLeftCell
            Pic.Left = .Left
            Pic.Top = .Top
        End With
    Next Pic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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