Error in Enableevents removing colon in military time

Ron Abraham

New Member
Joined
Dec 3, 2011
Messages
15
I have entered the following code in Excel 2013

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UserInput = Target.Value
If UserInput > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If

I am getting an error in "IF USERINPUT > 1 THEN". Can anyone help? Thanks!
 
I entered 1245 and pressed enter
The cell shows 0:00
I then up arrowed to the cell and it changed to 12:45
And then I got the error.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I entered 1245 and pressed enter
The cell shows 0:00
I then up arrowed to the cell and it changed to 12:45
And then I got the error.

It sounds like part of your problem is what I stated in my post #4.
You are using the SelectionChange event.

If you are in Cell A1 and you type something, then the box around Cell A1 moves to the next cell and if you start typing that next cell will now accept input.

The SelectionChange event doesn't take the value you typed before hitting enter and pass it to the Target.Value, it takes the value of the cell that the box moves into after you press enter.

I think you want the Change event. The change event takes the value that you typed into the original cell and passes it to the Target.Value.

I think this is probably at least part of your problem, but I'll let Rick be the final judge on that.
 
Upvote 0
When I run this code in the Change event and I type something like 300 in a cell it immediately returns 3:00 to the same cell I typed 300 in with no errors.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
UserInput = Target.Value
If UserInput > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
As Skywriter has pointed out, you are using the wrong event. Use Change not SelectionChange.

You also need to validate the data a lot more and also ensure that *no matter what* you execute the EnableEvents=True statement.

You also have to allow for integers with less than 4 digits as well as integers that are not times.

Try the lightly tested

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Dim Val: Val = Target.Value
    If Not Application.WorksheetFunction.IsNumber(Val) Then Exit Sub
    If Val < 0 Then Exit Sub
    If Val \ 1 <> Val Then Exit Sub
    If Val \ 100 > 23 Then Exit Sub
    If Val Mod 100 > 59 Then Exit Sub
    On Error Resume Next
    Application.EnableEvents = False
    Target.Value = Val \ 100 & ":" & Val Mod 100
    Application.EnableEvents = True
    End Sub
I have entered the following code in Excel 2013

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UserInput = Target.Value
If UserInput > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If

I am getting an error in "IF USERINPUT > 1 THEN". Can anyone help? Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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
Back
Top