In the block of code below, if I enter a value in G:G if the target cells offset(0, 2) =…
- User
In the block of code below, if I enter a value in G:G if the target cells offset(0, 2) = False, I then get a message "Date Service Completed not present". After acknowledging the message, I want the value of the Target cell to be restored to "". Can you show me how to adjust the code below to do this.
Set rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each cell In rng If IsNumeric(cell.Value) Then If IsDate(cell.Offset(0, 2).Value) = False Then If Not doubleClickFlag Then MsgBox "Date Service Completed not present" doubleClickFlag = False End If Exit Sub ElseIf cell.Offset(0, -5).Value <> "Service" Then Exit Sub
- Assistant
To restore the value of the target cell to "", you can add the code
cell.Value = ""after displaying the message in theIf Not doubleClickFlag Thencondition. Here's the adjusted code:vbSet rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each cell In rng If IsNumeric(cell.Value) Then If IsDate(cell.Offset(0, 2).Value) = False Then If Not doubleClickFlag Then MsgBox "Date Service Completed not present" doubleClickFlag = False cell.Value = "" ' Reset the value to "" End If Exit Sub ElseIf cell.Offset(0, -5).Value <> "Service" Then Exit Sub End If End If Next cell End IfNote: Make sure to adjust the indentation of the code according to your existing code structure.
- User
after entering the line of code 'cell.Value = “” ’ Reset the value to “”' the cell value is returned to "" but the codes loops and I can not stop the message until excel crashes
- Assistant
Apologies for the confusion. In order to prevent the code from looping and continuously displaying the message, you can set a flag variable to indicate whether the message has already been shown. Here's an updated version of the code:
vbSet rng = Intersect(Target, Range("G:G")) If Not rng Is Nothing Then For Each cell In rng If IsNumeric(cell.Value) Then If IsDate(cell.Offset(0, 2).Value) = False Then If Not doubleClickFlag Then MsgBox "Date Service Completed not present" doubleClickFlag = True ' Set flag to true cell.Value = "" ' Reset the value to "" End If Exit Sub ElseIf cell.Offset(0, -5).Value <> "Service" Then Exit Sub End If End If Next cell End If - User
the code is still looping
- Assistant
Apologies for the confusion. To prevent the code from looping, you can use a flag variable outside of the event handler to check whether the message has already been displayed. Here's an updated version of the code:
vb - User
- Assistant
Apologies for the oversight. The issue occurs because the variable
cellis declared in both the outer and inner loop. To resolve this, you can rename the inner loop variable to something different, such asinnerCell. Here's the updated code: - User
- Assistant