View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default worksheet_change event when multiple cells changed (pasted)

If target.Value = "" then

will raise an error if Target is a reference to multiple cells.

You have to adjust all the code to work with one or more cells.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
Dim cell as Range

if Time.count = 1 then
On Error GoTo EndMacro
Else
On Error Resume Next
End if
If Application.Intersect(Target, Range("A1:F600")) Is Nothing Then
Exit Sub
End If
'If Target.Cells.Count 1 Then
' Exit Sub
'End If
For each cell in Target Then
if cell.Value < "" then


Application.EnableEvents = False
With cell
If .HasFormula = False Then

Select Case Len(.Value)
Case 4 ' e.g., 123a = 1:23 a
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & " " & Right(.Value, 1)
Case 5 ' e.g., 1234a = 12:34 a
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & " " & Right(.Value, 1)
Case Else
'Err.Raise 0
TimeStr = "00:00"
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
End if
Next cell

Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy

"noddy26 " wrote in message
...
That's good news. Here is my code again:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String

On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:F600")) 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
With Target
If .HasFormula = False Then
' Do I need to insert a loop here?
Select Case Len(.Value)
Case 4 ' e.g., 123a = 1:23 a
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & " " & Right(.Value, 1)
Case 5 ' e.g., 1234a = 12:34 a
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & " " & Right(.Value, 1)
Case Else
Err.Raise 0
End Select
Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub


---
Message posted from http://www.ExcelForum.com/