Time macro

mfd2112

Board Regular
Joined
Jan 2, 2010
Messages
86
Hi,
I can’t remember how to have this time code work in two or more columns.
I think there should to be a “And” at the end of the line below? Then it should work, but that’s not it.
If Application.Intersect(Target, Range("E3:E940")) Is Nothing Then And _
If Application.Intersect(Target, Range("G3:G940")) Is Nothing Then
Exit Sub
End If
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Any help is appreciated
Dale



Code:
[SIZE=1]Private Sub Worksheet_Change(ByVal Target As Range)[/SIZE]
[SIZE=1]Dim TimeStr As String[/SIZE]
[SIZE=1]  On Error GoTo EndMacro[/SIZE]
[SIZE=1]  If Application.Intersect(Target, Range("E3:E940")) Is Nothing Then[/SIZE]
[SIZE=1]      Exit Sub[/SIZE]
[SIZE=1]  End If[/SIZE]
[SIZE=1]  If Target.Cells.Count > 1 Then Exit Sub[/SIZE]
[SIZE=1]  If Target.Value = "" Then Exit Sub[/SIZE]
[SIZE=1]  Application.EnableEvents = False[/SIZE]
[SIZE=1]  With Target[/SIZE]
[SIZE=1]      If .HasFormula = False Then[/SIZE]
[SIZE=1]          If .Value >= 1 Then[/SIZE]
[SIZE=1]              Select Case Len(.Value)[/SIZE]
[SIZE=1]                  Case 1 ' e.g., 1 = 01:00 AM[/SIZE]
[SIZE=1]                      TimeStr = Left(.Value, 2) & ":00"[/SIZE]
[SIZE=1]                  Case 2 ' e.g., 12 = 12:00 AM[/SIZE]
[SIZE=1]                      TimeStr = .Value & ":00"[/SIZE]
[SIZE=1]                  Case 3 ' e.g., 123 = 1:23 AM[/SIZE]
[SIZE=1]                      TimeStr = Left(.Value, 1) & ":" & _[/SIZE]
[SIZE=1]                      Right(.Value, 2)[/SIZE]
[SIZE=1]                  Case 4 ' e.g., 1234 = 12:34 AM[/SIZE]
[SIZE=1]                      TimeStr = Left(.Value, 2) & ":" & _[/SIZE]
[SIZE=1]                      Right(.Value, 2)[/SIZE]
[SIZE=1]                  Case Else[/SIZE]
[SIZE=1]                      Err.Raise 0[/SIZE]
[SIZE=1]              End Select[/SIZE]
[SIZE=1]              .Value = TimeValue(TimeStr)[/SIZE]
[SIZE=1]          End If[/SIZE]
[SIZE=1]          .NumberFormat = "h:mm;@"[/SIZE]
[SIZE=1]      End If[/SIZE]
[SIZE=1]  End With[/SIZE]
[SIZE=1]  Application.EnableEvents = True[/SIZE]
[SIZE=1]  Exit Sub[/SIZE]
[SIZE=1]EndMacro:[/SIZE]
[SIZE=1]  MsgBox "You did not enter a valid time. Please use figures only for the time e.g. 1030"[/SIZE]
[SIZE=1]  Application.EnableEvents = True[/SIZE]
[SIZE=1]End Sub[/SIZE]
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Code:
[SIZE=1] If Application.Intersect(Target, Range("E3:E940, G3:G940")) Is Nothing Then[/SIZE]
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,919
Members
452,949
Latest member
beartooth91

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