merging 2 sets of code

S

Steve R

I need some help "merging" the two sets of code listed
below. Each will work if they are listed alone on
the "view code" of the worksheet. I tried various ways to
make them work as one but failed.

The first one will insert the date and time in column A
if there is an entry made in column B.

The second one converts a 4 digit entry in a prescribed
range of cells to a time i.e. 2300 becomes 23:00

Thanks in advance for any help you can provide

Steve

_______________________________________________________
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsEmpty(Cells(Target.Row, 1)) Then Exit Sub
If Not IsEmpty(Target.Value) Then Cells(Target.Row, 1)
= Now
End Sub

______________________________________________________

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

On Error GoTo EndMacro
If Application.Intersect(Target, Range
("g11:h101,k11:k101,j2:k6")) 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
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
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
 
F

Frank Kabel

Hi Steve
try (untested)



Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
If not Application.Intersect(Target, Range ("B:B")) Is Nothing Then
If Not IsEmpty(Cells(Target.Row, 1)) Then Exit Sub
If Not IsEmpty(Target.Value) Then Cells(Target.Row, 1) = Now
end if

If Application.Intersect(Target, Range ("g11:h101,k11:k101,j2:k6")) 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
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
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
 
G

Guest

Frank, When i ran the code I got a syntax error on the
8th line of the code
(Below)

If Application.Intersect(Target, Range
("g11:h101,k11:k101,j2:k6")) Is
Nothing Then

Thanks for your quick reply to my original post

Steve
-----Original Message-----
Hi Steve
try (untested)



Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
If not Application.Intersect(Target, Range ("B:B")) Is Nothing Then
If Not IsEmpty(Cells(Target.Row, 1)) Then Exit Sub
If Not IsEmpty(Target.Value) Then Cells(Target.Row, 1) = Now
end if

If Application.Intersect(Target, Range
("g11:h101,k11:k101,j2:k6")) Is
 
F

Frank Kabel

Hi
this is your original line. This is only ONE line. So just remove the
linebreaks and it should work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top