In the event below, If I enable the lines 'If Not doubleClickFlag Then ' and…
- User
In the event below, If I enable the lines 'If Not doubleClickFlag Then ' and 'doubleClickFlag = False ' , then the following block of code does not run; ' If IsNumeric(innerCell.Value) Then If IsDate(innerCell.Offset(0, 2).Value) = False Then MsgBox "Date Service Completed not present" innerCell.Value = "" ' Reset the value to "" '
With the the following lines disabled 'If Not doubleClickFlag Then ' and 'doubleClickFlag = False ' , the following block code is looping continously; ' If IsNumeric(innerCell.Value) Then If IsDate(innerCell.Offset(0, 2).Value) = False Then MsgBox "Date Service Completed not present" innerCell.Value = "" ' Reset the value to "" '
Here is the complete event that contains the problems;
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Dim cell As Range Dim innerCell As Range Set rng = Intersect(Target, Range("I:I")) If Not rng Is Nothing Then For Each cell In rng If IsDate(cell.Value) Then If cell.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as the number of days." Exit Sub Else If Not doubleClickFlag Then MsgBox "Please change the Issue description (column B) as required." doubleClickFlag = False End If Exit Sub End If End If Next cell End If
Set rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each innerCell In rng 'If Not doubleClickFlag Then ' Check if message already displayed If IsNumeric(innerCell.Value) Then If IsDate(innerCell.Offset(0, 2).Value) = False Then MsgBox "Date Service Completed not present" innerCell.Value = "" ' Reset the value to "" Exit For ' Exit the loop 'doubleClickFlag = False ' Set flag to False ElseIf innerCell.Offset(0, -5).Value <> "Service" Then Exit For ' Exit the loop Else MsgBox "A New Service row will now be created" innerCell.Offset(0, -5).Value = "Serviced" innerCell.Offset(0, 1).Value = "" Dim newRow As Long newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" Range("C" & newRow).Value = innerCell.Offset(0, 2).Value Range("F" & newRow).Value = innerCell.Offset(0, -1).Value Range("J" & newRow).Value = innerCell.Offset(0, 3).Value Range("L" & newRow).Value = innerCell.Offset(0, 5).Value Range("H" & newRow).Value = innerCell.Offset(0, 2).Value + innerCell.Value Exit For ' Exit the loop 'End If End If End If Next innerCell End IfIf Target.Column = 5 Then ColumnList Target End If If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("I:I")) Is Nothing Then If IsDate(Target.Value) Then If Range("B" & Target.Row).Value <> "Request" Then Exit Sub Else MsgBox "Please change 'Request' to the relevant Task Issue" Range("B" & Target.Row).Select Exit Sub End If End If End If End Sub
- Assistant
- User
When I enter a value in innerCell.Value, the following code block does not run. This is evident because if the innerCell.Offset(0, 2).Value is False, the MsgBox does not appear. . . Set rng = Intersect(Target, Range(“G:G”)) If Not rng Is Nothing Then For Each innerCell In rng If Not doubleClickFlag Then ’ Check if message already displayed If IsNumeric(innerCell.Value) Then If IsDate(innerCell.Offset(0, 2).Value) = False Then MsgBox “Date Service Completed not present” innerCell.Value = “” ’ Reset the value to “” Exit For ’ Exit the loop 'doubleClickFlag = False ’ No need to update the flag here ElseIf innerCell.Offset(0, -5).Value <> “Service” Then Exit For ’ Exit the loop
- Assistant
Based on the updated information, it seems that the code block in question is not running because there might be an issue with the condition
IsNumeric(innerCell.Value).Possible reasons why the code block doesn't run:
- The value in
innerCell.Valuemay not be numeric, causing the code block to be skipped. Make sure that the value entered in is a valid number.
- The value in
- User
- Assistant
The error "Else without if" is occurring because there is an extra "End If" statement before the "Else" block in the code.
Here's the corrected code snippet for the problematic section:
Set rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each innerCell In rng If Not doubleClickFlag Then ' Check if message already displayed On Error Resume Next ' Add error handling If IsNumeric(innerCell.Value) Then If IsDate(innerCell.Offset(0, 2).Value) = False Then MsgBox "Date Service Completed not present" innerCell.Value = "" Exit For ' Exit the loop ElseIf innerCell.Offset(0, -5).Value <> "Service" Then Exit For ' Exit the loop ElseIf IsDate(innerCell.Offset(0, 2).Value) Then ' Add this condition MsgBox "A New Service row will now be created" innerCell.Offset(0, -5).Value = "Serviced" innerCell.Offset(0, 1).Value = "" ' Rest of the code… Exit For ' Exit the loop End If Else MsgBox "Invalid value in innerCell.Value" Exit For ' Exit the loop End If On Error GoTo 0 ' Reset error handling End If Next innerCell End If - User
Can this code be written better and more effective: If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("G6:G506")) Is Nothing Then
If Target.Value = "" Then Exit Sub
Else If Range("I" & Target.Row).Value = "" Then MsgBox "Date Service Completed not present" Target.Value = "" Exit Sub
Else If Range("B" & Target.Row).Value <> "Service" Then MsgBox "Task is not a current Service" Target.Value = "" Exit Sub
Else If Range("B" & Target.Row).Value = "Service" And Range("I" & Target.Row).Value <> "" Then MsgBox "A New Service row will now be created" Range("B" & Target.Row).Value = "Serviced" Range("H" & Target.Row).Value.Value = "" Dim newRow As Long newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" Range("C" & newRow).Value = Target.Offset(0, 2).Value 'column I dDate Completed Range("F" & newRow).Value = Target.Offset(0, -1).Value 'column F Location Range("J" & newRow).Value = Target.Offset(0, 3).Value 'column J Task Notes Range("L" & newRow).Value = Target.Offset(0, 5).Value 'column L Task details Range("H" & newRow).Value = Target.Offset(0, 2).Value + Target.Value 'column I + column G Exit Sub End If End If Next Target End If
- Assistant
Yes, the code can be written in a more concise and efficient way. Here's an improved version of the code:
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("G6:G506")) Is Nothing Then If Target.Value = "" Then Exit Sub If Range("I" & Target.Row).Value = "" Then MsgBox "Date Service Completed not present" Target.Value = "" Exit Sub End If If Range("B" & Target.Row).Value <> "Service" Then MsgBox "Task is not a current Service" Target.Value = "" Exit Sub End If MsgBox "A New Service row will now be created" Range("B" & Target.Row).Value = "Serviced" Range("H" & Target.Row).ClearContents Dim newRow As Long newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 With Range("B" & newRow) .Value = "Service" .Offset(0, 1).Value = Target.Offset(0, 2).Value 'Column I Date Completed .Offset(0, 5).Value = Target.Offset(0, -1).Value 'Column F Location .Offset(0, 9).Value = Target.Offset(0, 3).Value 'Column J Task