Excel macro suddenly stopped working. Please help!

endlesswonder 1 Reputation point
2022-06-17T03:13:48.343+00:00

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

Microsoft 365 and Office Excel For business Windows
Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. endlesswonder 1 Reputation point
    2022-06-18T01:24:06.197+00:00

    Got it to work. First went on safe mode to see if it was a problem with the code - it wasn't. Then I went into VBA and deleted all the modules and when I created a new one and added the same code, then edited it so Sub() was on top and the boolean was below that.

    I've done that before and things still didn't work but maybe deleting the mess in VBA with all the different module windows helped? I don't know. What I do know is that now it works and that's all I need.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.