PDA

View Full Version : Enable copy and paste



trevb
08-02-2009, 02:33 AM
I have added the following code to my spreadsheet to enable adding times without the colon but when I copy and paste a valid time from one cell to another it keeps going to the endmacro sub. Can anyone please help me to capture the copy and paste and then exit sub ?
thanks in advance


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("F6:I10")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
If Application.WorksheetFunction.IsText(Target.Value) Then
GoTo CapText
End If
With Target
If (.HasFormula = False) And (Not Application.WorksheetFunction.IsText(Target.Value)) Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
CapText:
If Not Application.Intersect(Target, Range("F6:I10")) Is Nothing Then
Target(1).Value = UCase(Target(1).Value)
End If
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did'nt enter a valid time, click OK and try again !!!" & _
vbCr & "Error code - " & Err.Number, vbExclamation, "Oooooops !!!"
Application.Undo
Application.EnableEvents = True
Exit Sub
End Sub

:banghead:

Bob Phillips
08-02-2009, 04:10 AM
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("F6:I10")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
If Application.WorksheetFunction.IsText(Target.Value) Then
GoTo CapText
End If
With Target
If (.HasFormula = False) And (Not Application.WorksheetFunction.IsText(Target.Value)) Then
.Value = Val(Replace(.Text, ":", ""))
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
.NumberFormat = "hh:mm:ss"
End If
End With
CapText:
If Not Application.Intersect(Target, Range("F6:I10")) Is Nothing Then
Target(1).Value = UCase(Target(1).Value)
End If
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did'nt enter a valid time, click OK and try again !!!" & _
vbCr & "Error code - " & Err.Number, vbExclamation, "Oooooops !!!"
Application.Undo
Application.EnableEvents = True
Exit Sub
End Sub

trevb
08-02-2009, 07:39 AM
Many thanks but it still doesn't work, it will allow copy and paste but if I enter a valid time i.e. 800 for 8:00AM it defaults to 0:00 and also allows incorrect times i.e. 5555