View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
ManicMiner17 ManicMiner17 is offline
external usenet poster
 
Posts: 30
Default VBA Validation between two Times

Andy,

This works whether the time is entered manually, or created using
drag-down to copy then pasted.

There seem to be subtle differences between times entered manually and
those which occur if the time is entered directly to the cell.

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False

Dim C22ValRange As Range
Dim DataOK As Boolean
Dim msg As String
Dim rCell As Range
Dim sCell As Range
Dim endCell As Range
Dim cellRow As Long

Dim Times(25) As Date


Times(1) = 0 '12AM
Times(2) = 4.16666666666666E-02 '1AM
Times(3) = 0.083333333 '2AM
Times(4) = 0.125 '3AM
Times(5) = 0.166666666666666 '4AM
Times(6) = 0.208333333333333 '5AM
Times(7) = 0.25 '6AM
Times(8) = 0.291666666666666 '7AM
Times(9) = 0.333333333333333 '8AM
Times(10) = 0.375 '9AM
Times(11) = 0.416666666666666 '10AM
Times(12) = 0.458333333333333 '11AM
Times(13) = 0.5 '12PM
Times(14) = 0.541666666666666 '1PM
Times(15) = 0.583333333333333 '2PM
Times(16) = 0.625 '3PM
Times(17) = 0.666666666666666 '4PM
Times(18) = 0.708333333333333 '5PM
Times(19) = 0.75 '6PM
Times(20) = 0.791666666666666 '7PM
Times(21) = 0.833333333333333 '8PM
Times(22) = 0.875 '9PM
Times(23) = 0.916666666666666 '10PM
Times(24) = 0.958333333333333 '11PM
Times(25) = 0.999999999999999

Set endCell = Cells(Rows.Count, "C").End(xlUp)

Set C22ValRange = Range("C11" & ":" & endCell.Address)

For Each rCell In C22ValRange
For Each sCell In Target
If rCell.Address = sCell.Address Then
MsgBox "Cell " & rCell.Address
cellRow = rCell.Row - 10
MsgBox cellRow
If Round(rCell.Value, 16) = Round(Times(cellRow), 16) And
rCell.Value <= Times(cellRow + 1) Then
Debug.Print rCell.Value
MsgBox "Valid Time " & rCell.Address
Else
MsgBox "Invalid Time " & rCell.Address
Debug.Print rCell.Value
rCell = ""
End If
End If
Next sCell
Next rCell
Application.EnableEvents = True
End Sub