Change Event not running

butts2805

New Member
Joined
Dec 7, 2009
Messages
10
Please can you give me some advice on why the following Change event will not work? It dose not seem to run.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
It is place in the worksheet module that it needs to work in.<o:p></o:p>
It is a procedure to convert an entry to military time format. EG 1800 become 18:00.<o:p></o:p>
Even when entering 2370, it will not bring up the error messages.<o:p></o:p>
<o:p></o:p>
It did previously work but now not. (Excel 2003)

<o:p>Private Sub Worksheet_Change(ByVal Target As Range)</o:p>
<o:p>Dim TimeStr As String
On Error Resume Next
If Application.Intersect(Target, Range("L4:GG6")) Is Nothing Then
Application.EnableEvents = False
Exit Sub
End If
If Target.Cells.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Not IsNumeric(Target.Value) Then Exit Sub

If Left(Target.Value, 2) > 23 Or Right(Target.Value, 2) > 59 Then
MsgBox "Please Enter A Valid Time value 0000 - 2359"

Target = ""
Target.Activate

Application.EnableEvents = True
Exit Sub
End If

Application.EnableEvents = False

With Target
UserInput = Format(Target.Value, "00:00")
If Len(UserInput) > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = UserInput
Application.EnableEvents = True
End If
End With
End Sub</o:p>

Many thanks

Malcolm
 
Last edited:

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Welcome to the Board.

If a cell outside the range L4:GG6 is changed this:

Code:
If Application.Intersect(Target, Range("L4:GG6")) Is Nothing Then
    Application.EnableEvents = False
    Exit Sub
End If

disables events so the code won't fire again. Try removing:

Application.EnableEvents = False

then go to the Immediate window, type:

Application.EnableEvents = True

and press Enter.
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,939
Welcome to the Board!

I think maybe it might have to do with the line in bold:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
<?xml:namespace prefix = o /><o:p>Dim TimeStr As String
On Error Resume Next
If Application.Intersect(Target, Range("L4:GG6")) Is Nothing Then
Application.EnableEvents = False
Exit Sub
End If
If Target.Cells.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Not IsNumeric(Target.Value) Then Exit Sub
 
If Left(Target.Value, 2) > 23 Or Right(Target.Value, 2) > 59 Then
MsgBox "Please Enter A Valid Time value 0000 - 2359"
 
Target = ""
Target.Activate
 
Application.EnableEvents = True
Exit Sub
End If
 
Application.EnableEvents = False
 
With Target
UserInput = Format(Target.Value, "00:00")
If Len(UserInput) > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = UserInput
Application.EnableEvents = True
End If
End With
End Sub
</o:p>
<o:p>I don't see why if you want to exit the sub, that you would disable the events prior to doing that?</o:p>
<o:p></o:p>
<o:p>So maybe your events are disabled and they just need to be re-eneabled.</o:p>
<o:p></o:p>
<o:p>Put this line in the immediate window and press enter:</o:p>
<o:p></o:p>
<o:p>
Rich (BB code):
Application.EnableEvents = True
</o:p><o:p>or just run this code:</o:p>
<o:p></o:p>
<o:p>
Rich (BB code):
sub test()</o:p>
<o:p></o:p><o:p>Application.EnableEvents = True
end sub
</o:p>
 

Watch MrExcel Video

Forum statistics

Threads
1,122,416
Messages
5,596,010
Members
414,037
Latest member
Roamingsmile

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
Top