In the VBA code below, a Start Date and an Ed Date are passed on in the following format…
- User
In the VBA code below, a Start Date and an Ed Date are passed on in the following format to the VBA code below. If either of the Start Date or End Date is not found in wsLeave, I want a message to pop up that says "The Leave Start Date or End Date" "is not within the curent 3 months" "Please check the dates entered", vbCritical, "DATE PERIOD" Exit Sub
Sub ProcessLeaveRequestJKL(wsLeave As Worksheet, firstMonthRange As Range, secondMonthRange As Range, Target As Range) Dim StartNumber As Long Dim StopNumber As Long Dim startDate As Date Dim endDate As Date Dim wsHolCard As Worksheet Dim startCol As Long Dim endCol As Long Dim cell As Range
Set wsHolCard = ThisWorkbook.Worksheets("HolCard") ' Record the Start Date and End Date from HolCard sheet startDate = wsHolCard.Cells(Target.row, "J").Value endDate = wsHolCard.Cells(Target.row, "K").Value ' Unprotect the 'Leave' sheet wsLeave.Unprotect Password:="edit" ' Find the corresponding column in 'Leave' sheet for Start Date For Each cell In wsLeave.Range("B27:CP27") If cell.Value = startDate Then startCol = cell.Column Exit For End If Next cell ' Find the corresponding column in 'Leave' sheet for End Date For Each cell In wsLeave.Range("B27:CP27") If cell.Value = endDate Then endCol = cell.Column Exit For End If Next cell ' Check if 'x' values already exist in the range B31:CP31 For i = startCol To endCol If wsLeave.Cells(31, i).Value = "x" Then MsgBox "Warning: 'x' values already exist in the range.", vbExclamation, "Data Conflict" Exit Sub End If Next i ' Write 'x' from Start Date to End Date in row 31 For i = startCol To endCol If wsLeave.Cells(27, i).Value <> "" Then ' Check if there is a date in row 27 wsLeave.Cells(31, i).Value = "x" wsLeave.Cells(35, i).Value = startDate wsLeave.Cells(35, i).Value = wsLeave.Cells(27, i).Value ' Use the date from row 27 End If Next i ' Protect the sheet 'Leave' wsLeave.Protect Password:="edit" ' Unprotect the 'HolCard' sheet, add a comment, and protect the sheet again wsHolCard.Unprotect Password:="edit" Target.AddComment "Leave dates recorded " & Date wsHolCard.Protect Password:="edit" MsgBox "Leave Dates Recorded", vInformation, "LEAVE RECORD"End Sub
- Assistant