Private Sub Worksheet_Change(ByVal Target As Range) Dim answer As VbMsgBoxResult Dim…
- User
Private Sub Worksheet_Change(ByVal Target As Range) Dim answer As VbMsgBoxResult Dim contractor As String Dim wpSheet As Worksheet Dim lastRow As Long
' Check if both E1 and G1 have values If Not IsEmpty(Range("E1")) And Not IsEmpty(Range("G1")) Then Exit Sub End If
' Check for text entered in column J If Target.Column = 10 And Target.Value <> "" Then ' Check if contractor is insured and DBS cleared Dim insured As Boolean Dim cleared As Boolean insured = Not IsEmpty(Range("E1")) ' check if E1 has a value cleared = Not IsEmpty(Range("G1")) ' check if G1 has a value
' Ask user if they want to send details to contractor If Not insured And Not cleared Then answer = MsgBox("Contractor is not Insured nor DBS cleared - would you like to send details?", vbYesNo + vbQuestion, "Send details") ElseIf Not insured Then answer = MsgBox("Contractor is not Insured - would you like to send details?", vbYesNo + vbQuestion, "Send details") ElseIf Not cleared Then answer = MsgBox("Contractor is not DBS cleared - would you like to send details?", vbYesNo + vbQuestion, "Send details") Else answer = MsgBox("Would you like to send details to the contractor?", vbYesNo + vbQuestion, "Send details") End If
If answer = vbYes Then ' Store details from current sheet in variables contractor = ActiveSheet.Name ' If you have other details to store, add them here
' Activate Work Permit sheet Set wpSheet = ThisWorkbook.Sheets("Work Permit") wpSheet.Activate ' Copy data from previous sheet lastRow = wpSheet.Cells(wpSheet.Rows.Count, "A").End(xlUp).Row + 1
wpSheet.Cells(lastRow, "A").Value = Target.Value wpSheet.Cells(lastRow, "B").Value = ThisWorkbook.Sheets(contractor).Range("B1").Value wpSheet.Cells(lastRow, "C").Value = ThisWorkbook.Sheets(contractor).Cells(Target.Row, "C").Value
End If End If End Sub This vba code does not run as expected. What is the error
- Assistant
There is an error in the first If statement that checks if both E1 and G1 have values. The condition should be "And" instead of "Or". The corrected code is:
' Check if both E1 and G1 have values If Not IsEmpty(Range("E1")) And Not IsEmpty(Range("G1")) Then Exit Sub End If
- User
There is no Or in the code I presented
- Assistant
You are correct, my apologies. I misread the code.
The issue with the code is that the
Private Sub Worksheet_Change(ByVal Target As Range)event is only triggered when a change is made to the worksheet. The code is checking for changes in column J (If Target.Column = 10) and only runs if a value is entered in that column (Target.Value <> ""), which might not always be the case.If you want the code to run when any cell in the worksheet is changed, you need to remove the
If Target.Column = 10 And Target.Value <> ""condition.Alternatively, if you only want the code to run when column J is changed, you need to make sure that the event is triggered. One way to do this is to add the following line to the top of the code:
If Intersect(Target, Range("J:J")) Is Nothing Then Exit Sub.This will check if the changed cell is in column J, and if not, exit the code.
- User
My original code below that only checks column 10 and target value works perfectly. The addition to check E1 and G1 appear to be what is stopping the new code from working. Can you fix this. Private Sub Worksheet_Change(ByVal Target As Range) Dim answer As VbMsgBoxResult Dim contractor As String Dim wpSheet As Worksheet Dim lastRow As Long ' Check for text entered in column J If Target.Column = 10 And Target.Value <> "" Then ' Ask user if they want to send details to contractor answer = MsgBox("Would you like to send details to the contractor?", vbYesNo + vbQuestion, "Send details") If answer = vbYes Then ' Store details from current sheet in variables contractor = ActiveSheet.Name ' If you have other details to store, add them here
' Activate Work Permit sheet Set wpSheet = ThisWorkbook.Sheets("Work Permit") wpSheet.Activate ' Copy data from previous sheet lastRow = wpSheet.Cells(wpSheet.Rows.Count, "A").End(xlUp).Row + 1
wpSheet.Cells(lastRow, "A").Value = Target.Value wpSheet.Cells(lastRow, "B").Value = ThisWorkbook.Sheets(contractor).Range("B1").Value wpSheet.Cells(lastRow, "C").Value = ThisWorkbook.Sheets(contractor).Cells(Target.Row, "C").Value
End If End If End Sub
- Assistant
In that case, you can remove the check for E1 and G1 by deleting the following lines:
' Check if both E1 and G1 have values If Not IsEmpty(Range("E1")) And Not IsEmpty(Range("G1")) Then Exit Sub End If
This should allow the code to run as expected when a user enters a value in column J.
- User
There are formulas in E1 and G1. Maybe this is the problem. How can the code check for the value presented and not be confused by the formula
- Assistant
If there are formulas in E1 and G1 and you only want the code to check for their current values, you can modify the code to check for the current values of the cells instead of whether they are empty. Here's an example that checks the current values of E1 and G1:
' Check if both E1 and G1 have values If Range("E1").Value = "" Or Range("G1").Value = "" Then Exit Sub End IfThis will ignore any formulas and only check for the current values of the cells.
- User
what about this part of the code. I want it to check for values only. Dim insured As Boolean Dim cleared As Boolean insured = Not IsEmpty(Range("E1")) ' check if E1 has a value cleared = Not IsEmpty(Range("G1")) ' check if G1 has a value
- Assistant
To check for the current values of E1 and G1, you can modify the code as follows:
Dim insured As Boolean Dim cleared As Boolean insured = (Range("E1").Value <> "") ' check if E1 has a value cleared = (Range("G1").Value <> "") ' check if G1 has a valueThis will check the current values of E1 and G1, ignoring any formulas that may be in those cells.
- User
Private Sub Worksheet_Change(ByVal Target As Range) Dim answer As VbMsgBoxResult Dim contractor As String Dim wpSheet As Worksheet Dim lastRow As Long
' Check if both E1 and G1 have values If Range("E1").Value = "" Or Range("G1").Value = "" Then Exit Sub End If
' Check for text entered in column J If Target.Column = 10 And Target.Value <> "" Then ' Check if contractor is insured and DBS cleared Dim insured As Boolean Dim cleared As Boolean insured = (Range("E1").Value <> "") ' check if E1 has a value cleared = (Range("G1").Value <> "") ' check if G1 has a value
' Ask user if they want to send details to contractor If Not insured And Not cleared Then answer = MsgBox("Contractor is not Insured nor DBS cleared - would you like to send details?", vbYesNo + vbQuestion, "Send details") ElseIf Not insured Then answer = MsgBox("Contractor is not Insured - would you like to send details?", vbYesNo + vbQuestion, "Send details") ElseIf Not cleared Then answer = MsgBox("Contractor is not DBS cleared - would you like to send details?", vbYesNo + vbQuestion, "Send details") Else answer = MsgBox("Would you like to send details to the contractor?", vbYesNo + vbQuestion, "Send details") End If
- Assistant
There could be a couple of reasons why the code is not running. One possibility is that the code is not located in the correct worksheet module. Make sure that the code is placed in the module for the worksheet where you want it to run.
Another possibility is that there is a syntax error in the code or an issue with the workbook that is preventing it from running. Try the following steps to debug the code:
-
Add the line
MsgBox "Code is running"at the beginning of the procedure to verify whether the code is being triggered when a change is made to the worksheet. -
Check to make sure that the variables
Target,contractor, andwpSheetare being assigned the correct values. You can do this by addingMsgBoxstatements that display the values of these variables at various points in the code.
For example, you can add the following line after the line
contractor = ActiveSheet.Name: -