View Single Post
 
Old 05-26-2016, 01:09 AM
Philb1 Philb1 is offline Windows 10 Office 2010 32bit
Advanced Beginner
 
Join Date: Feb 2016
Location: Auckland
Posts: 43
Philb1 is on a distinguished road
Default

You should turn off enable events because unless the code causes it, it prevents the macro from getting stuck in a infinite loop and as it's an event change macro, it prevents the change from being picked up when the macro has made any change to the worksheet causing it to run again.. I removed the cellcontent variable, it's not needed and added error control to ensure enable events is always switched back on.
Otherwise it works as it should on my computer
Cheers
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim myRange As Range
Dim cellLen As Long

On Error GoTo TheExit ' Error control

Set myRange = Range("E:E")

    If Not Intersect(Target, myRange) Is Nothing Then
    
            Application.EnableEvents = False ' Prevents inadvertent looping etc
            cellLen = Len(Target)
        
            If (cellLen < 5) Then
                Target.HorizontalAlignment = xlCenter
            Else
                Target.HorizontalAlignment = xlLeft
            End If
            
        GoTo TheExit
        
    Else
    
        GoTo TheExit
    
    End If

TheExit:
Application.EnableEvents = True
Exit Sub

End Sub
Reply With Quote