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
If Inclusive Then
Select Case targetVal
Case Is < lowVal
Case Is > hiVal
Case Else
isInBetween = True
End Select
If inDebug Then MsgBox "Testing if " & lowVal & " <= " & targetVal & " <= " & hiVal & vbCrLf & vbCrLf & "Result = " & isInBetween
Else
Select Case targetVal
Case Is <= lowVal
Case Is >= hiVal
Case Else
isInBetween = True
End Select
If inDebug Then MsgBox "Testing if " & lowVal & " < " & targetVal & " < " & hiVal & vbCrLf & vbCrLf & "Result = " & isInBetween
End If
End Function