Guys
With the assistance of SHG, Parsnip, Dave & Daddylonglegs I have manage to put together a little app that assist our staff with scheduling dates in a calendar to avoid a specific trend.
Code provided by SHG has worked perfectly; see below:
Code
Public DaysOff As Range
Function datNext(datLast As Date, DaysOff As Range) As Date
Dim datBeg As Date, datEnd As Date
Dim iLastPd As Integer
Dim iDay As Long, nDay As Long
Dim iYr As Integer, iMo As Integer
iYr = Year(datLast)
iMo = Month(datLast)
datBeg = DateSerial(iYr, iMo + 1, 1)
datEnd = DateSerial(iYr, iMo + 2, 0)
iLastPd = Pd(datLast)
nDay = DaysOff.Rows.Count
Do
Do
datNext = Int((datEnd - datBeg + 1) * Rnd + datBeg)
Loop Until Pd(datNext) <> iLastPd
For iDay = 1 To nDay
If DaysOff(iDay) = datNext Then Exit For
Next
Loop Until iDay > nDay
End Function
Function Pd(dat As Date) As Integer
Select Case Day(dat)
Case 1 To 10: Pd = 1
Case 11 To 20: Pd = 2
Case Else: Pd = 3
End Select
End Function
Display More
Though the built in DAY function on the calendar is affecting the dates that are calculated cos the function PD also references this. Whenever I change the year or month on the calendar; it changes the UDF, which is not supposed to be. If the recommended dates are going change all the time; as a result of this, it makes this app useless.
How do I get around this? Also is this what is causing the UDF to recalculate?