Insert Colon and Still Have Date in the Same Column

jsnyder57

New Member
Joined
Mar 21, 2013
Messages
7
Hi All I have a scheduler that contains a date and several rows containing time in the same column. I am currently using the following VBA code to insert a colon in the correct spot

Private Sub Worksheet_ChangeDate(ByVal Target As Range)
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
UserInput = Target.Value
If UserInput > 99
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If
End Sub

And the code inputs a colon in between the 20 and 13 for the year. I tried using the next code, but none of the code works (we tried using msgboxes to test the code and none of the boxes popped up)

Private Sub Worksheet_ChangeDate(ByVal Target As Range)
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
UserInput = Target.Value
If UserInput > 99 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
ElseIf UserInput = 2013 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & "/" & Mid(UserInput, 2) & "/" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If
End Sub

Does anyone have any suggestions to make this code work?

Thanks
 
Wow thank you so much Scott and Rick for the great code/info. I am just now learning VBA and I am trying to goto it as much as possible now (Less likely for my users to corrupt my formulas). MrExcel has been a great place for me to go find answers/solutions.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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