Private Sub Worksheet_Activate()
Application.EnableEvents = False
ActiveSheet.Calculate
Application.EnableEvents = True
Dim myRange As Range
Set myRange = ThisWorkbook.Sheets("Job Request").Range("A2:F2")
If WorksheetFunction.CountA(myRange) > 0 Then
Dim message As String
message = "Do you want to delete the Request Entry?"
Dim response As VbMsgBoxResult
response = MsgBox(message, vbYesNo + vbQuestion, "Delete contents?")
If response = vbYes Then
Application.Calculate
ThisWorkbook.Sheets("Job Request").Range("A2:F2").ClearContents
End If
Else
Exit Sub
End If
End Sub
Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("B2:E2")) Is Nothing Then Exit Sub 'Currently within B2:F2
If Not Intersect(Target, Range("F2")) Is Nothing Then 'Currently in F2
GoTo COMPLETE
If Not Intersect(Target, Range("A2")) Is Nothing Then 'Currently in A2
Application.Calculate
Application.Wait (Now + TimeValue("0:00:02"))
Dim wscf As Worksheet
Dim adminRequestCell As Range
Dim isAdminRequestPresent As Boolean
Const ADMIN_REQUEST_EXISTS_MSG As String = "Admin Request has already been sent. Do you want to RE-SEND?"
Set wscf = Worksheets(Range("I2").Value)
Set adminRequestCell = wscf.Range("B:B").Find("Admin Request", , xlValues, xlWhole, xlByColumns)
If Not adminRequestCell Is Nothing Then 'Admin Request Found
answer = MsgBox(ADMIN_REQUEST_EXISTS_MSG, vbYesNo + vbQuestion, "Resend details")
If answer = vbNo Then
GoTo COMPLETE
End If
ElseIf adminRequestCell Is Nothing Then 'Admin Request NOT Found
GoTo CHECKS
End If
CHECKS:
Const INSURANCE_REQUEST_MSG As String = "Contractor is not insured. Create INSURANCE Request?"
Const INSURANCE_REQUEST As String = "INSURANCE"
Const DBS_REQUEST_MSG As String = "Contractor has not been DBS’d. Create DBS Request?"
Const DBS_REQUEST = "DBS"
Const INSURANCE_DBS_REQUEST_MSG As String = "Contractor is not insured and has not been DBS’d. Create INSURANCE/DBS Request?"
Const INSURANCE_DBS_REQUEST As String = "INS/DBS"
'Dim answer As VbMsgBoxResult
Dim insured As Boolean
Dim cleared As Boolean
Dim contractor As String
Dim exists As String
'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").Value
If insured And cleared Then 'Insurance and DBS are both complete
Exit Sub
If Not insured And Not cleared Then 'Insurance and DBS are both not complete
answer = MsgBox(Format(INSURANCE_DBS_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details")
If answer = vbYes Then
Dim wsjr As Worksheet
Dim wswp As Worksheet
Dim lastRow As Long
Set wsjr = Worksheets("Job Request")
Set wswp = Worksheets("Work Permit")
lastRow = wswp.Cells(Rows.Count, "A").End(xlUp).Row + 1
wswp.Range("A" & lastRow).Value = INSURANCE_DBS_REQUEST
wswp.Range("B" & lastRow).Value = wsjr.Range("A2").Value
wswp.Range("C" & lastRow).Value = Date
wswp.Range("D" & lastRow).Value = "Admin Request"
Call RequestToContractorXYZ
Exit Sub
If Not insured Then 'only Insurnace is not complete
answer = MsgBox(Format(INSURANCE_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details")
If answer = vbYes Then
'Dim wsjr As Worksheet
'Dim wswp As Worksheet
'Dim lastRow As Long
'Set wsjr = Worksheets("Job Request")
'Set wswp = Worksheets("Work Permit")
lastRow = wswp.Cells(Rows.Count, "A").End(xlUp).Row + 1
wswp.Range("A" & lastRow).Value = INSURANCE_REQUEST
wswp.Range("B" & lastRow).Value = wsjr.Range("A2").Value
wswp.Range("C" & lastRow).Value = Date
workPermitSheet.Range("D" & lastRow).Value = "Admin Request"
Call RequestToContractorXYZ
Exit Sub
If Not cleared Then 'only DBS is not complete
answer = MsgBox(Format(DBS_REQUEST_MSG, contractor), vbYesNo + vbQuestion, "Send details")
If answer = vbYes Then
'Dim wsjr As Worksheet
'Dim wswp As Worksheet
'Dim lastRow As Long
'Set wsjr = Worksheets("Job Request")
'Set wswp = Worksheets("Work Permit")
lastRow = wswp.Cells(Rows.Count, "A").End(xlUp).Row + 1
wswp.Range("A" & lastRow).Value = DBS_REQUEST
wswp.Range("B" & lastRow).Value = wsjr.Range("A2").Value
wswp.Range("C" & lastRow).Value = Date
wswp.Range("D" & lastRow).Value = "Admin Request"
Call RequestToContractorXYZ
Exit Sub
COMPLETE:
If Not Intersect(Target, Range("B2:E2")) Is Nothing Then Exit Sub 'Currently within B2:F2
If Application.CountA(Range("A2:F2")) = 6 Then
Call RequestToContractorXYZ
If Application.CountA(Range("A2:F2")) <> 6 Then
MsgBox ("All fields need to be filled in")
SelectFirstEmptyCell
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
Private Sub SelectFirstEmptyCell()
Dim wsas As Worksheet
Dim nextCell As Range
Set wsas = ActiveSheet
Application.EnableEvents = False
Set nextCell = wsas.Range("A2:F2").Find("", , xlValues, xlWhole) ' Find the first empty cell (exact matching) within the range A2:E2
If Not nextCell Is Nothing Then ' If an empty cell is found, select it
nextCell.Select
wsas.Range("F2").Value = ""
End If
Application.EnableEvents = True
End Sub
Sub RequestToContractorXYZ()
Dim wsjrx As Worksheet
Dim wscfx As Worksheet
Set wsjrx = Worksheets("Job Request")
Set wscfx = Worksheets(Range("I2").Value)
Dim lastRow As Long
lastRow = wscfx.Cells(wscfx.Rows.Count, "B").End(xlUp).Row + 1
wscfx.Range("B" & lastRow).Value = wsjrx.Range("B2").Value
wscfx.Range("C" & lastRow).Value = wsjrx.Range("F2").Value
wscfx.Range("D" & lastRow).Value = wsjrx.Range("A3").Value
wscfx.Range("F" & lastRow).Value = wsjrx.Range("C2").Value
wscfx.Range("J" & lastRow).Value = wsjrx.Range("E2").Value
wscfx.Range("L" & lastRow).Value = wsjrx.Range("D2").Value
Application.ScreenUpdating = False
wsjrx.Range("A2").ClearContents
wsjrx.Range("B2").ClearContents
wsjrx.Range("C2").ClearContents
wsjrx.Range("D2").ClearContents
wsjrx.Range("E2").ClearContents
wsjrx.Range("F2").ClearContents
wsjrx.Range("E5:E20").ClearContents
wscfx.Select
Application.ScreenUpdating = True
End Sub
Sub Last_Ten_Jobs()
Dim wscf As Worksheet
Dim wsjr As Worksheet
Dim lastRow As Long
Dim copyRange As Range
Application.Wait (Now + TimeValue("0:00:02"))
Set wscf = Sheets(Range("I2").Value)
Set wsjr = Sheets("Job Request")
Application.ScreenUpdating = False
Application.EnableEvents = False
'Find the last non-empty row in the source sheet column J
lastRow = wscf.Cells(Rows.Count, "J").End(xlUp).Row
'Set the range to be copied
If lastRow > 10 Then
Set copyRange = wscf.Range("J" & lastRow - 9 & ":J" & lastRow)
Else
Set copyRange = wscf.Range("J4:J" & lastRow)
End If
'Copy the range to the destination sheet column E
wsjr.Range("E5").Resize(copyRange.Rows.Count, 1).Value = copyRange.Value
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Deactivate()
Range("E5:E20").ClearContents
End Sub