Sub LateCancel()
Dim Sh As Worksheet, Service As String, LCPrice As Currency, answer As String, n As Long, mth As String
Dim lr As Long, r As Long, t As Long
Set Sh = Sheets("Totals")
'values on totals sheet that the user is looking for
Dim LCReq As String: LCReq = Sh.Cells(32, 2).Value
Dim LCDt As String: LCDt = CDate(Sh.Cells(37, 2).Value)
Dim LateCancelHours As String: LateCancelHours = Sh.Cells(35, 2).Value
mth = MonthName(Month(LCDt))
'If the date of the job in the calculator of sheet2 is after the 26th, assign the month where it goes to the following month
If Day(LCDt) >= 26 Then mth = MonthName(Month(LCDt) + 1)
Worksheets(mth).Activate
n = 0
t = 0
lr = Cells(Rows.Count, "A").End(xlUp).Row
'Start in row 4 as that's where the data starts
For r = 4 To lr
If Cells(r, 1).Value <> LCDt And Cells(r, 3).Value <> LCReq Then t = t + 1
If Cells(r, 1).Value = LCDt And Cells(r, 3).Value = LCReq Then
'store the service in the service variable.
If Cells(r, 5) = "" Then
MsgBox "There is a job in row " & r & " on the " & mth & " sheet that matches the date and " & _
"request number but does not have a service type. Please add a valid service type before continuing."
Exit Sub
End If
Service = Cells(r, 5).Value
Range("A" & r & ":" & "O" & r).Interior.ColorIndex = 6
answer = MsgBox("Is this the job you want to apply the late cancel price too?", vbQuestion + vbYesNo + vbDefaultButton2, "Late Cancel Price")
Rows(r).Interior.ColorIndex = 0
If answer = vbNo Then
'Add 1 to the counter of If No is pressed
n = n + 1
End If
If answer = vbYes Then
If Cells(r, 5) = "Carer Respite" Then
MsgBox "Carer respite cannot have the Late Cancel price applied to it."
Exit Sub
End If
With Data
.Cells(30, 1) = CDate(LCDt)
.Cells(30, 2) = Service
'Set the hourly figure in the lateCanel table to be the LateCancelHours variable
.Cells(30, 5) = LateCancelHours
'A late cancel will be charged for 1 staff member attending
'Therefore, set the Staff Req. figure to 1
.Cells(30, 6) = 1
'Calculates price of late cancel on worksheet so the new price will be copied to the allocation sheet instead of the previous price
Calculate
LCPrice = .Cells(30, 8).Value
End With
Dim LTCnclDate As String
Cells(r, 1).Value = "LT CNCL " & Cells(r, 1).Value
Cells(r, 8).Value = LCPrice
Cells(r, 9).Formula = "=IF(RC[-4]=""Activities"",0,RC[-1]*0.1)"
Cells(r, 10).Formula = "=RC[-1]+RC[-2]"
Exit Sub
End If
Else
End If
Next r
If n > 0 Then MsgBox "You have chosen to not apply the late cancel price to any of the " & n & " jobs matching the date and request number."
If t > 0 Then MsgBox "There is no job with the date: " & LCDt & " and the request number: " & LCReq & " in the sheet of " & mth & "."
Sh.Activate
End Sub