I am getting a bBlock if without end if error in this code: Private Sub…
- User
I am getting a bBlock if without end if error in this code: 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, -5).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 Exit Sub
If Not Intersect(Target, Range("G6:G506")) Is Nothing Then
For Each Target In Range("G6:G506")
If Target.Value = "" Then Exit For Else ' Check if the value in the cell two columns to the right of the current cell is empty Dim rightCell As Range Set rightCell = Target.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 "" Target.Value = "" End If End IfNext Target Exit Sub
If Target.Offset(0, -5).Value <> "Service" Then MsgBox "Task is not a current Service" Target.Value = "" 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 SubEnd If
End Sub
- Assistant
- User
When I enter a value into column G, none of the conditions relating to the entry work.
- Assistant
- User
When I enter a value into column G and the value in column I of the sam row is empty, I get the message 'Date Service Completed not present'. But if column I has a date value in it and I then enter a value into column G, the expected message 'A New Service row will now be created' does not run.
- Assistant
- User
- Assistant
Apologies for the confusion. The issue lies in the condition
If Target.Offset(0, -7).Value = "Service". It seems that the formula is incorrectly referencing column B using offset -7. Let's correct it by changing the offset value to -5, which corresponds to column B. Here's the corrected code: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, -5).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 Application.EnableEvents = False ' Disable events to prevent triggering another change event 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 "" cell.Value = "" End If End If Next cell Application.EnableEvents = True