Hi,
I'm encountering a problem with running a macro in excel. The macro allows me to select a picture and when using the shortcut, it would center the image in the cell that its in. The macro works perfectly - it even works in the exact worksheet I'm encountering the problem with. The problem is: the macro seems to work on cells in Rows 7 and above but doesn't in the cells in the Rows below 7. And this is happening in ALL of the worksheets. The first worksheet I used it on was perfectly fine and then now, even in that worksheet, this same problem occurs.
I've done the basics - ctrl+g on vba window and deleted the cells, cleared the contents, etc. There are no error messages that pop up or even the error sound. I press the shortcut and a loading sign appears over the cursor for a split second and then goes back to being a cursor and the image stays where it is. It seems that the code is working but not visually executing? Can someone help me with this please - I've been struggling with trying to find an answer for days.
This is the code (taken from someone else):
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