Can you please correct this code. Private Sub WorksheetChange(ByVal Target As Range)…
- User
Can you please correct this code. Private Sub Worksheet_Change(ByVal Target As Range) Const INSURANCE_REQUEST_MSG As String = "Contractor is is not insured. Create INSURANCE Request?" Const DBS_REQUEST_MSG As String = "Contractor is has not been DBS’d. Create DBS Request?" Const INSURANCE_DBS_REQUEST_MSG As String = "Contractor is not insured and has not been DBS’d. Create INSURANCE/DBS Request?" Dim answer As VbMsgBoxResult Dim insured As Boolean Dim cleared As Boolean Dim contractor As String
'CHECK AND MAKE SURE ONLY ONE CELL IS SELECTED If Target.CountLarge > 1 Then Exit Sub 'CHECK IF CHANGE HAPPENED IN A2 If Not Intersect(Target, Range("A2")) Is Nothing Then Application.EnableEvents = False ActiveSheet.Calculate 'recalculate sheet Application.EnableEvents = True
'IF CHANGE HAPPENED IN A2 THEN SHOW RELEVANT MESSAGE DEPENDING ON THE VALUES IN G3 AND H3 insured = (Range("G3").Value <> "") cleared = (Range("H3").Value <> "") contractor = Range("B3") If Not insured And Not cleared Then answer = MsgBox(Format(INSURANCE_DBS_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details") If answer = vbYes Then Sheets("Work Permit").Select Exit Sub ElseIf Not insured Then 'only G3 is empty answer = MsgBox(Format(INSURANCE_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details") If answer = vbYes Then Sheets("Work Permit").Select Exit Sub ElseIf Not cleared Then 'only H3 is empty answer = MsgBox(Format(DBS_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details") If answer = vbYes Then Sheets("Work Permit").Select Exit Sub Else 'both G3 and H3 have values 'IF ANSWER IS YES MOVE TO THE WORK PERMIT SHEET AND EXIT THE SUB 'IF ANSWER IS NO REMAIN IN CURRENT SHEET 'IF CHANGE HAPPENED IN F2 THEN CHECK IF ALL 6 CELLS IN A2:F2 HAVE VALUES If Not Intersect(Target, Range("F2")) Is Nothing Then If Application.CountA(Range("A2:F2")) = 6 Then 'WHEN ALL CELLS HAVE VALUES CALL THE REQUEST SUB Call Request ElseIf Application.CountA(Range("A2:F2")) <> 6 Then 'SHOW PROMPT "ALL FIELDS NEED TO BE FILLED IN" MsgBox ("All Fields need to be filled in") SelectFirstEmptyCell End If End If End If End If End If End If End If End Sub
- Assistant
- User
I do not have any values in B3. Is there a reason for it in the code
- Assistant
Yes, the code uses the value in cell B3 to include in the message boxes that ask the user if they want to create an insurance request, DBS request, or both. If there is no value in B3, these messages will not include the contractor's name, so they may not make sense.
If you don't want to include the contractor's name in the messages, you can remove the reference to cell B3 in the Format function calls for the message boxes like this:
answer = MsgBox(INSURANCE_DBS_REQUEST_MSG, vbYesNo + vbQuestion, "Send details")answer = MsgBox(INSURANCE_REQUEST_MSG, vbYesNo + vbQuestion, "Send details")answer = MsgBox(DBS_REQUEST_MSG, vbYesNo + vbQuestion, "Send details")This will still show the same messages, but without the contractor's name.
- User
The contractors name is in A2
- Assistant
In that case, you can replace the line:
contractor = Range("B3").Valuewith:
contractor = Range("A2").ValueThis will ensure that the contractor's name is taken from cell A2 instead of B3.
- User
I need a vba code that will take concatenated values of F3 and G3 and H3 from Sheet Job Request and put it into the next emmpty cell in column A of Sheet Work Permit, and in the adjoining column B of the same row of Sheet Work Permit get the values of A2 from Sheet Job Request, and column C of the same row in Sheet Work Permit enter todays date and in column D the text "PERMIT REQUEST"
- Assistant
Here is a code that will copy the concatenated values of F3, G3, and H3 from the "Job Request" sheet to the next empty row of column A in the "Work Permit" sheet. It will then add the value of A2 from the "Job Request" sheet to column B, today's date to column C, and the text "PERMIT REQUEST" to column D in the same row of "Work Permit" sheet:
Sub CopyToWorkPermit() Dim jobRequestSheet As Worksheet Dim workPermitSheet As Worksheet Dim lastRow As Long Dim concatValues As String 'Assign the Job Request and Work Permit sheets to variables Set jobRequestSheet = Worksheets("Job Request") Set workPermitSheet = Worksheets("Work Permit") 'Get the concatenated values from F3, G3, and H3 of Job Request sheet concatValues = jobRequestSheet.Range("F3").Value & " " & jobRequestSheet.Range("G3").Value & " " & jobRequestSheet.Range("H3").Value 'Get the next empty row in column A of Work Permit sheet lastRow = workPermitSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1 'Copy the concatenated values to column A of Work Permit sheet in the next empty row workPermitSheet.Range("A" & lastRow).Value = concatValues 'Get the value of A2 from Job Request sheet Dim contractor As String contractor = jobRequestSheet.Range("A2").Value 'Enter the value of A2 to column B, today's date to column C, and "PERMIT REQUEST" to column D in the same row of Work Permit sheet workPermitSheet.Range("B" & lastRow).Value = contractor workPermitSheet.Range("C" & lastRow).Value = Date workPermitSheet.Range("D" & lastRow).Value = "PERMIT REQUEST" End Sub - User
In the full code Sub CopyToWorkPermit() further down below, where it has: 'Get the concatenated values from F3, G3, and H3 of Job Request sheet concatValues = jobRequestSheet.Range("F3").Value & " " & jobRequestSheet.Range("G3").Value & " " & jobRequestSheet.Range("H3").Value . . .
What I would like is if G3 in Sheet Job Request is empty insert the text "Insurance details required" and put it into the next empty row in column A of Work Permit sheet. Or If H3 is empty insert the text "DBS details required" put it into the next empty row in column A of Work Permit sheet. And If both G3 and H3 are empty insert the text "Insurance & DBS details required" put it into the next empty row in column A of Work Permit sheet . . . The rest of the code to stay the same.
Sub CopyToWorkPermit() Sheets("Work Permit").Select Dim jobRequestSheet As Worksheet Dim workPermitSheet As Worksheet Dim lastRow As Long Dim concatValues As String
- Assistant
Here's the modified code that includes the changes you requested:
Sub CopyToWorkPermit() Sheets("Work Permit").Select Dim jobRequestSheet As Worksheet Dim workPermitSheet As Worksheet Dim lastRow As Long Dim concatValues As String 'Assign the Job Request and Work Permit sheets to variables Set jobRequestSheet = Worksheets("Job Request") Set workPermitSheet = Worksheets("Work Permit") 'Check value of G3 and H3 of Job Request sheet If jobRequestSheet.Range("G3").Value = "" And jobRequestSheet.Range("H3").Value = "" Then 'Both G3 and H3 are empty concatValues = "Insurance & DBS details required" ElseIf jobRequestSheet.Range("G3").Value = "" Then 'G3 is empty concatValues = "Insurance details required" ElseIf jobRequestSheet.Range("H3").Value = "" Then 'H3 is empty concatValues = "DBS details required" Else 'Both G3 and H3 have values 'Get the concatenated values from F3, G3, and H3 of Job Request sheet concatValues = jobRequestSheet.Range("F3").Value & " " & jobRequestSheet.Range("G3").Value & " " & jobRequestSheet.Range("H3").Value End If 'Get the next empty row in column A of Work Permit sheet lastRow = workPermitSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1 'Copy the concatenated values to column A of Work Permit sheet in the next empty row workPermitSheet.Range("A" & lastRow).Value = concatValues 'Get the value of A2 from Job Request sheet Dim contractor As String contractor = jobRequestSheet.Range("A2").Value 'Enter the value of A2 to column B, today’s date to column C, and "PERMIT REQUEST" to column D in the same row of Work Permit sheet workPermitSheet.Range("B" & lastRow).Value = contractor workPermitSheet.Range("C" & lastRow).Value = Date workPermitSheet.Range("D" & lastRow).Value = "PERMIT REQUEST" End Sub - User
wss.Range("F" & lastRow).Value = ws.Range("D1").Value . . . in this line of code, I want the wss.Range("F" & lastRow).Value = "Admin Request" . . . Will this change reflect a text value of "Admin Request"
- Assistant
Yes, that modification will insert the text value "Admin Request" in column F of the last row in the
wssworksheet.The line of code
wss.Range("F" & lastRow).Value = ws.Range("D1").Valueis using the value in cell D1 of thewsworksheet to fill column F in thewssworksheet. If you want to replace this value with a fixed text like "Admin Request", you can simply change the code to:wss.Range("F" & lastRow).Value = "Admin Request"This code will insert the text value "Admin Request" in the last row of column F of the
wssworksheet.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.