The following code is not reacting to a specific condition. When I enter a value in…
- User
The following code is not reacting to a specific condition. When I enter a value in column I, and the value in the same row of column B is equal to "Service", it is bring up the message "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days." When I then enter a value in the same row of column G, it should then bring up the message "A New Service row will now be created". The code is not bringing up this message and therefore not completing.
Private Sub Worksheet_Change(ByVal Target As Range) Dim rngI As Range Dim rngG As Range Dim cell As Range Dim innercell As Range
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("I6:I506")) Is Nothing Then If IsDate(Target.Value) Then If Target.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days." Range("G" & Target.Row).Select Exit Sub Else MsgBox "Please change the Issue description ( column B ) as required." Range("B" & Target.Row).Select Exit Sub End If End If End If If Not Intersect(Target, Range("G6:G506")) Is Nothing Then For Each cell In Intersect(Target, Range("G6:G506")) If cell.Value <> "" Then ' Check if the value in the cell two columns to the right of the current cell is empty Dim rightCell As Range Set rightCell = cell.Offset(0, 2) If rightCell.Value = "" Then ' Display a message box MsgBox "Date Service Completed not present" ' Set the value in the current cell to "" Application.EnableEvents = False cell.Value = "" Application.EnableEvents = True End If End If Next cell Exit Sub ' Move this Exit Sub statement here End If If Target.Offset(0, -5).Value <> "Service" Then MsgBox "Task is not a current Service" Application.EnableEvents = False Target.Value = "" Application.EnableEvents = True Exit Sub End If If Target.Offset(0, -5).Value = "Service" And Target.Offset(0, 2).Value <> "" Then MsgBox "A New Service row will now be created" Target.Offset(0, -5).Value = "Serviced" Target.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 = 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 IfEnd Sub
- Assistant
It's difficult to determine the exact issue without more information or being able to test the code. However, there are a few potential areas that could be causing the problem. Here are some things to check:
-
Confirm that the code is located in the correct worksheet module. Make sure it is in the module of the worksheet where you want the code to run.
-
Verify that the range references in the code are correct. For example, check if the range "I6:I506" and "G6:G506" are the correct ranges for your data.
-
Make sure there are no typos or spelling errors in the condition checks. Double-check that the value being compared to in the If statements ("Service" and "") matches the exact values in column B and G.
-
If the issue persists, try adding some debugging statements to the code to see if it's actually being triggered. For example, you can add a MsgBox or Debug.Print statement at different points in the code to see if it executes and what the values of certain variables are.
Additionally, it's worth noting that the use of Select and Activate is generally not recommended in VBA. Instead of using Range("G" & Target.Row).Select and Range("B" & Target.Row).Select, consider using Range("G" & Target.Row).Value and Range("B" & Target.Row).Value directly to read or write values to cells.
-