I want to include in my VBA code, a routine that checks Column B for its last entry. If…
- User
I want to include in my VBA code, a routine that checks Column B for its last entry. If the last entry in Column B contains the text value PERMIT REQUEST, the the rest of the VBA code is skipped
- Assistant
Here's a sample code that does what you're looking for:
codeSub CheckColumnB() Dim lastRow As Long lastRow = Cells(Rows.Count, "B").End(xlUp).Row If InStr(1, Range("B" & lastRow).Value, "PERMIT REQUEST") > 0 Then MsgBox "Skipping the rest of the code." Exit Sub End If 'Rest of your code comes here End SubThis code checks the last non-empty cell in Column B and if it contains the text "PERMIT REQUEST", it displays a message box and exits the subroutine without executing the rest of the code. If the last entry in Column B does not contain "PERMIT REQUEST", it executes the rest of the code.
You can modify the code to suit your specific needs. Hope this helps!
- User
I inserted your suggestion in the vba code below but it does not react to the text PERMIT REQUEST which I have inserted in column B. . . Private Sub Worksheet_Change(ByVal Target As Range) Dim answer As VbMsgBoxResult Dim contractor As String Dim wpSheet As Worksheet Dim lastRow As Long
lastRow = Cells(Rows.Count, "B").End(xlUp).Row If InStr(1, Range("B" & lastRow).Value, "PERMIT REQUEST") > 0 Then MsgBox "Skipping the rest of the code." 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("Not Insured No DBS - Create INSURANCE/DBS Request?", vbYesNo + vbQuestion, "Send details") ElseIf Not insured Then answer = MsgBox("Contractor not Insured - Create INSURANCE Request?", vbYesNo + vbQuestion, "Send details") ElseIf Not cleared Then answer = MsgBox("Contractor not DBS'd - Create DBS Request?", vbYesNo + vbQuestion, "Send details") Else: Exit Sub 'Else 'answer = MsgBox("Would you like to send details to the contractor?", vbYesNo + vbQuestion, "Send details") End If
- Assistant
It looks like the code you provided should work correctly with my suggestion inserted. Can you confirm that you have actually typed "PERMIT REQUEST" (without quotes) in the last cell of Column B and that the text is not surrounded by any leading or trailing spaces or other characters?
Also, make sure that the worksheet containing this code is where Column B is located.
If the issue still persists, please provide more details on the problem you're facing, such as error messages or unexpected behavior.
- User
Yes I have the text PERMIT REQUEST in the last the last cell of Column B and that the text is not surrounded by any leading or trailing spaces or other characters
- Assistant
If the code is not working, it is possible that there's an issue with the event that triggers the Worksheet_Change event handler. The Worksheet_Change event fires when cells on the worksheet are changed by the user or VBA code.
To make sure that the Worksheet_Change event is properly triggered, I recommend adding a debugging statement right at the beginning of the event handler that displays the changed cells whenever the event fires.
Here's the modified code with the debugging statement added:
code