In my sheet 'Booking' I have a range that is a five month calendar. the Range is C2:J36.…
- User
In my sheet 'Booking' I have a range that is a five month calendar. the Range is C2:J36. Column C is used to identify the month using the first day of the month. C2:C8 for the current month C9:C15 for the next month after C16:C22 for the next month C23:C29 for the second to the last month C30:C36 for the last of the five months. D2:J2 contains text values Mon to Sun D3:J8 contains the days for the month shown in C2 for the cells in D3:J8 that do not contain dates they have a text value '.' D9:J9 contains text values Mon to Sun D10:J15 contains the days for the month shown in C9 for the cells in D10:J15 that do not contain dates they have a text value '.' D16:J16 contains text values Mon to Sun D17:J22 contains the days for the month shown in C16 for the cells in D17:J22 that do not contain dates they have a text value '.' D23:J23 contains text values Mon to Sun D24:J29 contains the days for the month shown in C23 for the cells in D24:J29 that do not contain dates they have a text value '.' D30:J30 contains text values Mon to Sun D31:J36 contains the days for the month shown in C30 for the cells in D31:J36 that do not contain dates they have a text value '.'
When I enter a date value in A8 the vba code is supposed to check the range C2:J36 for any cell that has a background colour of Red and a date value equals to A8. I have discovered that if the date value of A8 is in the current month and the same date in range C3:J8 has a background of Red, the VBA identifies the condition and it pops up the required message. If however the date value of A8 is for another month and the same date in the corresponding range in C to J has a background of Red, the VBA does not identy the condition and therefore the pop up is not actioned.
Can you detect what is causing this, the VBA is below:
' Check if the changed cell is A8 If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, wsBooking.Range("A8")) Is Nothing Then ThisWorkbook.Sheets("Booking").Unprotect Password:="edit" Application.EnableEvents = False Dim startDate As Date Dim cell2 As Range Dim clashFound As Boolean ' Flag to indicate if a clash is found
startDate = wsBooking.Range("A8").Value clashFound = False ' Initialize the flag ' Adjust the range for D2:J36 appropriately For Each cell2 In wsBooking.Range("D2:J36") If cell2.Value = startDate And cell2.Interior.Color = RGB(255, 0, 0) Then clashFound = True ' Set flag to true if a clash is found Exit For ' Exit loop once a clash is found (optional) End If Next cell2 If clashFound Then Dim response As VbMsgBoxResult response = MsgBox("The start date already has a booking." & vbCrLf & vbCrLf & _ "Check Period of booking in Asset Event." & vbCrLf & vbCrLf & _ "Yes to continue and add date." & vbCrLf & _ "No to check periods do not clash.", _ vbCritical + vbYesNo, "DATE CLASH") If response = vbNo Then wsBooking.Range("A8").ClearContents Application.EnableEvents = True Exit Sub End If End If ' Clear other ranges regardless of the clash wsBooking.Range("A10").ClearContents wsBooking.Range("A12").ClearContents wsBooking.Range("A14:A25").ClearContents wsBooking.Range("A27").ClearContents Application.EnableEvents = True ThisWorkbook.Sheets("Booking").Protect Password:="edit" - Assistant