View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Brian Handly Brian Handly is offline
external usenet poster
 
Posts: 3
Default Worksheet_Change: Date Time Input [mmddyy hhmm] to [mm/dd/yy

Dave Peterson wrote:
How about this:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

'MMDDYY HHMM
Dim myRng As Range
Dim myCell As Range
Dim myMonth As Long
Dim myDay As Long
Dim myYear As Long
Dim myHour As Long
Dim myMin As Long

Set myRng = Me.Range("a1:A999")

If Intersect(myRng, Target) Is Nothing Then
Exit Sub
End If

For Each myCell In Intersect(myRng, Target).Cells
With myCell
If .Value Like "###### ####" Then
'keep going
myMonth = Mid(.Value, 1, 2)
myDay = Mid(.Value, 3, 2)
myYear = Mid(.Value, 5, 2)
If myYear < 31 Then
myYear = myYear + 2000
Else
myYear = myYear + 1900
End If
myHour = Mid(.Value, 8, 2)
myMin = Mid(.Value, 10, 2)

If Format(DateSerial(myYear, myMonth, myDay) _
+ TimeSerial(myHour, myMin, 0), "mmddyy hhmm") _
= .Value Then
'it's ok
Application.EnableEvents = False
.Value = DateSerial(myYear, myMonth, myDay) _
+ TimeSerial(myHour, myMin, 0)
.NumberFormat = "mmddyy hhmm"
Application.EnableEvents = True
End If
End If
End With
Next myCell

End Sub

=======
And for the .formula vs .value in Chip's code. I'd just say he wrote each
procedure at different times.

Since he does this:

If .HasFormula = False Then

He knows that the .value and .formula return the exact same thing.

Brian Handly wrote:
WinXP SP2, Excel XP

I found Chip Pearson's Date Time help:
(http://www.cpearson.com/excel/DateTimeEntry.htm)

However, his Worksheet_Charge example VBA Subroutines are either for
Dates or Times.

I need to handle Date + Time in the same field.

I have tried to combine Chip's two example Subroutines into one, but
have not managed to make a single Subroutine that will handle inputs
like "MMDDYY HHMM" [preferred input]

I have not been able to figure out why Chip's Datevalue Sub uses
".Formula" and the TimeValue Sub uses ".Value" when extracting the user
input.

Suggestions?

Texas Handly


Dave

Thanks, Especially for your response on .Formula vs .Value

I will try the Subroutine.

Texas Handly