PC Review


Reply
Thread Tools Rate Thread

Combine 2 codes from WorkSheet_Change & WorkSheet _SelectionChange to ONLY WorkSheet_Change

 
 
Corey
Guest
Posts: n/a
 
      17th Dec 2006
There is NO problem with the Code 1, it works great.
Code 2 does work, but the user needs to Re-Enter the cell for the msgbox to
appear.
I need it to work if the user leaves the cell, and a value is found that
requires the msgbox to appear works.

So i was wondering if there is a way BOTH codes can be utilised within the
WorkSheet_Change and
yet both operate correctly then ?



Code 1:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Worksheet_Change(ByVal target As Excel.Range)
' This Code with allow the user to input Times as a 730, 1800 value in the
designated range, and convert to actual AM/PM Times....
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(target,
Range("C7:C8,C11:C12,C15:C16,F7:F8,F11:F12,F15:F16,I7:I8,I11:I12,I15:I16,L7:L8,L11:L12,L15:L16,O7:O8,O11:O12,O15:O16,R7:R8,R11:R12,R15:R16,U7:U8,U11:U12,U15:U16,V2:X2"))
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 ' this is code

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 "Not a Valid Time !!!" & vbCrLf & vbCrLf & vbTab & "Enter Times in as
a 24hr format." & vbCrLf & vbCrLf & vbTab & vbTab & vbTab & "EG. 0730 & 1530
format !!!", , "...."
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code 2:
Private Sub Worksheet_SelectionChange(ByVal target As Range)
' This code with check if there is a Time in RANGE1 that is < a Time Value
in RANGE2 provided it is in the same Column....
Const WS_RANGE1 As String =
"C11,C15,F11,F15,I11,I15,L11,L15,O11,O15,R11,R15,U11,U15"
Const WS_RANGE2 As String =
"C8,C12,F8,F12,I8,I12,L8,L12,O8,O12,R8,R12,U8,U12"

Const msg As String = _
"There is an overlap in the Times Entered." & vbNewLine & _
"The next Start Time needs to be equal or greater than the previous
Finish Time."

If Not Intersect(target, Range(WS_RANGE1)) Is Nothing Then
If target.Value = "" Or target.Offset(-3, 0).Value = "" Then
Exit Sub
End If
If target.Offset(-3, 0).Value > target.Value And _
target.Offset(-2, 0).Value <> Range("V17").Value Then
MsgBox msg, , "...."
target.Offset(0, 0).ClearContents
target.Offset(0, 0).Select
End If
ElseIf Not Intersect(target, Range(WS_RANGE2)) Is Nothing Then
If target.Value = "" Or target.Offset(-2, 0).Value = "" Then
Exit Sub
End If
If target.Value < target.Offset(-2, 0).Value And _
target.Value < Range("V17").Value Then
MsgBox msg, , "...."
target.ClearContents
target.Select
End If
End If

End Sub


Corey....


 
Reply With Quote
 
 
 
 
Tom Ogilvy
Guest
Posts: n/a
 
      17th Dec 2006
Perhaps somethingl like this:

Private Sub Worksheet_Change(ByVal target As Excel.Range)
' This Code with allow the user to input Times as a 730, 1800 value in the
designated range, and convert to actual AM/PM Times....
Dim TimeStr As String
On Error GoTo EndMacro
If target.Cells.Count > 1 Then
Exit Sub
End If

If Not Application.Intersect(target,
Range("C7:C8,C11:C12,C15:C16,F7:F8," &
"F11:F12,F15:F16,I7:I8,I11:I12,I15:" & _
"I16,L7:L8,L11:L12,L15:L16,O7:O8,O11" & _
":O12,O15:O16,R7:R8,R11:R12,R15:R16," & _
"U7:U8,U11:U12,U15:U16,V2:X2"))
Is Nothing Then


If not target.Value = "" Then
Application.EnableEvents = False
On Error Resume Next
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
if err.Number = 0 then
.Value = TimeValue(TimeStr)
else
MsgBox "Not a Valid Time !!!" & vbCrLf & vbCrLf & vbTab & "Enter Times
in as
a 24hr format." & vbCrLf & vbCrLf & vbTab & vbTab & vbTab & "EG. 0730 & 1530
format !!!", , "...."
err.clear
end if
End If


End With
Application.EnableEvents = True

End if


' This code with check if there is a Time in RANGE1 that is < a Time Value
in RANGE2 provided it is in the same Column....
Const WS_RANGE1 As String =
"C11,C15,F11,F15,I11,I15,L11,L15,O11,O15,R11,R15,U11,U15"
Const WS_RANGE2 As String =
"C8,C12,F8,F12,I8,I12,L8,L12,O8,O12,R8,R12,U8,U12"

Const msg As String = _
"There is an overlap in the Times Entered." & vbNewLine & _
"The next Start Time needs to be equal or greater than the previous
Finish Time."

If Not Intersect(target, Range(WS_RANGE1)) Is Nothing Then
If target.Value = "" Or target.Offset(-3, 0).Value = "" Then
Exit Sub
End If
If target.Offset(-3, 0).Value > target.Value And _
target.Offset(-2, 0).Value <> Range("V17").Value Then
MsgBox msg, , "...."
target.Offset(0, 0).ClearContents
target.Offset(0, 0).Select
End If
ElseIf Not Intersect(target, Range(WS_RANGE2)) Is Nothing Then
If target.Value = "" Or target.Offset(-2, 0).Value = "" Then
Exit Sub
End If
If target.Value < target.Offset(-2, 0).Value And _
target.Value < Range("V17").Value Then
MsgBox msg, , "...."
target.ClearContents
target.Select
End If
End If

End Sub


--
Regards,
Tom Ogilvy

"Corey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> There is NO problem with the Code 1, it works great.
> Code 2 does work, but the user needs to Re-Enter the cell for the msgbox
> to appear.
> I need it to work if the user leaves the cell, and a value is found that
> requires the msgbox to appear works.
>
> So i was wondering if there is a way BOTH codes can be utilised within the
> WorkSheet_Change and
> yet both operate correctly then ?
>
>
>
> Code 1:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Private Sub Worksheet_Change(ByVal target As Excel.Range)
> ' This Code with allow the user to input Times as a 730, 1800 value in the
> designated range, and convert to actual AM/PM Times....
> Dim TimeStr As String
> On Error GoTo EndMacro
> If Application.Intersect(target,
> Range("C7:C8,C11:C12,C15:C16,F7:F8,F11:F12,F15:F16,I7:I8,I11:I12,I15:I16,L7:L8,L11:L12,L15:L16,O7:O8,O11:O12,O15:O16,R7:R8,R11:R12,R15:R16,U7:U8,U11:U12,U15:U16,V2:X2"))
> 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 ' this is code
>
> 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 "Not a Valid Time !!!" & vbCrLf & vbCrLf & vbTab & "Enter Times in
> as a 24hr format." & vbCrLf & vbCrLf & vbTab & vbTab & vbTab & "EG. 0730 &
> 1530 format !!!", , "...."
> End Sub
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Code 2:
> Private Sub Worksheet_SelectionChange(ByVal target As Range)
> ' This code with check if there is a Time in RANGE1 that is < a Time Value
> in RANGE2 provided it is in the same Column....
> Const WS_RANGE1 As String =
> "C11,C15,F11,F15,I11,I15,L11,L15,O11,O15,R11,R15,U11,U15"
> Const WS_RANGE2 As String =
> "C8,C12,F8,F12,I8,I12,L8,L12,O8,O12,R8,R12,U8,U12"
>
> Const msg As String = _
> "There is an overlap in the Times Entered." & vbNewLine & _
> "The next Start Time needs to be equal or greater than the previous
> Finish Time."
>
> If Not Intersect(target, Range(WS_RANGE1)) Is Nothing Then
> If target.Value = "" Or target.Offset(-3, 0).Value = "" Then
> Exit Sub
> End If
> If target.Offset(-3, 0).Value > target.Value And _
> target.Offset(-2, 0).Value <> Range("V17").Value Then
> MsgBox msg, , "...."
> target.Offset(0, 0).ClearContents
> target.Offset(0, 0).Select
> End If
> ElseIf Not Intersect(target, Range(WS_RANGE2)) Is Nothing Then
> If target.Value = "" Or target.Offset(-2, 0).Value = "" Then
> Exit Sub
> End If
> If target.Value < target.Offset(-2, 0).Value And _
> target.Value < Range("V17").Value Then
> MsgBox msg, , "...."
> target.ClearContents
> target.Select
> End If
> End If
>
> End Sub
>
>
> Corey....
>
>



 
Reply With Quote
 
Corey
Guest
Posts: n/a
 
      17th Dec 2006
Thank you Tom.
Beautiful.

Corey....
"Tom Ogilvy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Perhaps somethingl like this:
>
> Private Sub Worksheet_Change(ByVal target As Excel.Range)
> ' This Code with allow the user to input Times as a 730, 1800 value in the
> designated range, and convert to actual AM/PM Times....
> Dim TimeStr As String
> On Error GoTo EndMacro
> If target.Cells.Count > 1 Then
> Exit Sub
> End If
>
> If Not Application.Intersect(target,
> Range("C7:C8,C11:C12,C15:C16,F7:F8," &
> "F11:F12,F15:F16,I7:I8,I11:I12,I15:" & _
> "I16,L7:L8,L11:L12,L15:L16,O7:O8,O11" & _
> ":O12,O15:O16,R7:R8,R11:R12,R15:R16," & _
> "U7:U8,U11:U12,U15:U16,V2:X2"))
> Is Nothing Then
>
>
> If not target.Value = "" Then
> Application.EnableEvents = False
> On Error Resume Next
> 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
> if err.Number = 0 then
> .Value = TimeValue(TimeStr)
> else
> MsgBox "Not a Valid Time !!!" & vbCrLf & vbCrLf & vbTab & "Enter
> Times in as
> a 24hr format." & vbCrLf & vbCrLf & vbTab & vbTab & vbTab & "EG. 0730 &
> 1530
> format !!!", , "...."
> err.clear
> end if
> End If
>
>
> End With
> Application.EnableEvents = True
>
> End if
>
>
> ' This code with check if there is a Time in RANGE1 that is < a Time Value
> in RANGE2 provided it is in the same Column....
> Const WS_RANGE1 As String =
> "C11,C15,F11,F15,I11,I15,L11,L15,O11,O15,R11,R15,U11,U15"
> Const WS_RANGE2 As String =
> "C8,C12,F8,F12,I8,I12,L8,L12,O8,O12,R8,R12,U8,U12"
>
> Const msg As String = _
> "There is an overlap in the Times Entered." & vbNewLine & _
> "The next Start Time needs to be equal or greater than the previous
> Finish Time."
>
> If Not Intersect(target, Range(WS_RANGE1)) Is Nothing Then
> If target.Value = "" Or target.Offset(-3, 0).Value = "" Then
> Exit Sub
> End If
> If target.Offset(-3, 0).Value > target.Value And _
> target.Offset(-2, 0).Value <> Range("V17").Value Then
> MsgBox msg, , "...."
> target.Offset(0, 0).ClearContents
> target.Offset(0, 0).Select
> End If
> ElseIf Not Intersect(target, Range(WS_RANGE2)) Is Nothing Then
> If target.Value = "" Or target.Offset(-2, 0).Value = "" Then
> Exit Sub
> End If
> If target.Value < target.Offset(-2, 0).Value And _
> target.Value < Range("V17").Value Then
> MsgBox msg, , "...."
> target.ClearContents
> target.Select
> End If
> End If
>
> End Sub
>
>
> --
> Regards,
> Tom Ogilvy
>
> "Corey" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> There is NO problem with the Code 1, it works great.
>> Code 2 does work, but the user needs to Re-Enter the cell for the msgbox
>> to appear.
>> I need it to work if the user leaves the cell, and a value is found that
>> requires the msgbox to appear works.
>>
>> So i was wondering if there is a way BOTH codes can be utilised within
>> the WorkSheet_Change and
>> yet both operate correctly then ?
>>
>>
>>
>> Code 1:
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Private Sub Worksheet_Change(ByVal target As Excel.Range)
>> ' This Code with allow the user to input Times as a 730, 1800 value in
>> the designated range, and convert to actual AM/PM Times....
>> Dim TimeStr As String
>> On Error GoTo EndMacro
>> If Application.Intersect(target,
>> Range("C7:C8,C11:C12,C15:C16,F7:F8,F11:F12,F15:F16,I7:I8,I11:I12,I15:I16,L7:L8,L11:L12,L15:L16,O7:O8,O11:O12,O15:O16,R7:R8,R11:R12,R15:R16,U7:U8,U11:U12,U15:U16,V2:X2"))
>> 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 ' this is code
>>
>> 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 "Not a Valid Time !!!" & vbCrLf & vbCrLf & vbTab & "Enter Times in
>> as a 24hr format." & vbCrLf & vbCrLf & vbTab & vbTab & vbTab & "EG. 0730
>> & 1530 format !!!", , "...."
>> End Sub
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Code 2:
>> Private Sub Worksheet_SelectionChange(ByVal target As Range)
>> ' This code with check if there is a Time in RANGE1 that is < a Time
>> Value in RANGE2 provided it is in the same Column....
>> Const WS_RANGE1 As String =
>> "C11,C15,F11,F15,I11,I15,L11,L15,O11,O15,R11,R15,U11,U15"
>> Const WS_RANGE2 As String =
>> "C8,C12,F8,F12,I8,I12,L8,L12,O8,O12,R8,R12,U8,U12"
>>
>> Const msg As String = _
>> "There is an overlap in the Times Entered." & vbNewLine & _
>> "The next Start Time needs to be equal or greater than the previous
>> Finish Time."
>>
>> If Not Intersect(target, Range(WS_RANGE1)) Is Nothing Then
>> If target.Value = "" Or target.Offset(-3, 0).Value = "" Then
>> Exit Sub
>> End If
>> If target.Offset(-3, 0).Value > target.Value And _
>> target.Offset(-2, 0).Value <> Range("V17").Value Then
>> MsgBox msg, , "...."
>> target.Offset(0, 0).ClearContents
>> target.Offset(0, 0).Select
>> End If
>> ElseIf Not Intersect(target, Range(WS_RANGE2)) Is Nothing Then
>> If target.Value = "" Or target.Offset(-2, 0).Value = "" Then
>> Exit Sub
>> End If
>> If target.Value < target.Offset(-2, 0).Value And _
>> target.Value < Range("V17").Value Then
>> MsgBox msg, , "...."
>> target.ClearContents
>> target.Select
>> End If
>> End If
>>
>> End Sub
>>
>>
>> Corey....
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worksheet_Change - initiate macro on another worksheet oms Microsoft Excel Programming 4 11th Mar 2008 03:26 PM
Can I have two worksheet_change events in same worksheet? =?Utf-8?B?a2x5c2VsbA==?= Microsoft Excel Programming 5 28th Aug 2007 05:32 PM
Call Worksheet_Change macro in another worksheet =?Utf-8?B?RnJlZGR5?= Microsoft Excel Programming 6 29th Oct 2006 07:16 PM
Re: worksheet_change vs. calculate, and worksheet_change not running Tom Ogilvy Microsoft Excel Programming 1 14th Jul 2003 02:51 AM
worksheet_change vs. calculate, and worksheet_change not running Ross Microsoft Excel Programming 0 13th Jul 2003 04:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:23 PM.