ForbiddenOrc
New Member
- Joined
- Aug 17, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Hello,
I am extremely new at VBA coding and am trying to learn in order to make an aspect of my job much easier and less time consuming for my team. I have a spreadsheet that is used to track appointments. Reminders need to be sent 2 days before these appointments. I'm at the point where my macro can pull all of the data from my sheet, check the language, time zone, etc.. and generate reminders for me. All I have to do is click send. I'm surprised I've even been able to get here with the little knowledge I have of VBA. But I've hit a wall. I have a check that looks if the appointment is 2 days away and it mostly works except for 1 little thing that I can't figure out. So the reminders are sent 48 hours/2 days prior to the appointment time and this check needs to exclude weekends. When I run my macro today on Thursday, it generates reminders for tomorrow and for Monday which is perfect and what I want it to do. BUT, it also generates a reminder for Friday next week which is much more that 2 days away. I've tried so many different variations at this point and I'm at my wits end. Would anyone know how I can correct my function to NOT send a reminder for this upcoming Friday? For reference here is how I want the reminder needed times to be structured:
Monday appointment should generate a reminder starting on Thursday the week prior
Tuesday appointment should generate a reminder starting on Friday the week prior
Wednesday appointment should generate a reminder starting on Monday the same week
Thursday appointment should generate a reminder starting on Tuesday the same week
Friday appointment should generate a reminder starting on the Wednesday same week
Here is the code for my function. I tried separating my Thursday and Friday so they were handled separately but it always ends up breaking my code even more.
I am extremely new at VBA coding and am trying to learn in order to make an aspect of my job much easier and less time consuming for my team. I have a spreadsheet that is used to track appointments. Reminders need to be sent 2 days before these appointments. I'm at the point where my macro can pull all of the data from my sheet, check the language, time zone, etc.. and generate reminders for me. All I have to do is click send. I'm surprised I've even been able to get here with the little knowledge I have of VBA. But I've hit a wall. I have a check that looks if the appointment is 2 days away and it mostly works except for 1 little thing that I can't figure out. So the reminders are sent 48 hours/2 days prior to the appointment time and this check needs to exclude weekends. When I run my macro today on Thursday, it generates reminders for tomorrow and for Monday which is perfect and what I want it to do. BUT, it also generates a reminder for Friday next week which is much more that 2 days away. I've tried so many different variations at this point and I'm at my wits end. Would anyone know how I can correct my function to NOT send a reminder for this upcoming Friday? For reference here is how I want the reminder needed times to be structured:
Monday appointment should generate a reminder starting on Thursday the week prior
Tuesday appointment should generate a reminder starting on Friday the week prior
Wednesday appointment should generate a reminder starting on Monday the same week
Thursday appointment should generate a reminder starting on Tuesday the same week
Friday appointment should generate a reminder starting on the Wednesday same week
Here is the code for my function. I tried separating my Thursday and Friday so they were handled separately but it always ends up breaking my code even more.
Excel Formula:
Function IsReminderNeeded(appointmentDate As Date) As Boolean
Dim currentDate As Date
currentDate = Date
'Calculate the number of days between the appointment day and the current day
Dim dayDiff As Integer
dayDiff = (AppointmentWeekday(appointmentDate) - AppointmentWeekday(currentDate) + 7) Mod 7
If dayDiff = 3 Or dayDiff = 4 Then
'Thursday or Friday appointments:
IsReminderNeeded = (appointmentDate - currentDate) <= 4 And (appointmentDate - currentDate) >= 0
ElseIf dayDiff = 0 Then
'Monday appointments
IsReminderNeeded = (appointmentDate - currentDate) <= 10 And (appointmentDate - currentDate) >= 0
ElseIf dayDiff = 1 Then
'Tuesday appointments
IsReminderNeeded = (appointmentDate - currentDate) <= 11 And (appointmentDate - currentDate) >= 0
ElseIf dayDiff = 2 Then
'Wednesday appointments
IsReminderNeeded = (appointmentDate - currentDate) <= 2 And (appointmentDate - currentDate) >= 0
Else
'Other cases: No reminder needed
IsReminderNeeded = False
End If
End Function
Excel Formula:
Function AppointmentWeekday(appointmentDate As Date) As Integer
Dim weekdayNumber As Integer
weekdayNumber = Weekday(appointmentDate, vbMonday)
If weekdayNumber = 7 Then
weekdayNumber = 1 'Adjust Sunday to be considered as Monday
Else
weekdayNumber = weekdayNumber + 1 'Shift other weekdays
End If
AppointmentWeekday = weekdayNumber
End Function