DushiPunda
Well-known Member
- Joined
- Nov 14, 2015
- Messages
- 518
- Office Version
- 2021
- Platform
- Windows
Hey all,
I'm working on creating a new schedule for my job, to include a sheet for various stats. One of the stats I'm looking at is how many months a given worker was available based on their certification date ("cd") and whether or not they have transferred from/otherwise left the job ("td"). To do this, I've created a function MAvail (Months Available) to calculate how many months a worker was available based on a few conditions/logic. However, it seems my work network is not a fan of macros, so it only works as long as I don't close the file after creating the macro. I'm wondering if there is a formula that can help me calculate the months available.
Here is the code for the function I've created (to be clear, it does work, my work network is not cooperating):
I should explain a little of my set up:
- There are 13 tabs/worksheets in the workbook. 12 of those are schedules for each month. One of them is a "Stats" sheet.
- Cell A1 (.Cells(1, 1)) on each sheet is linked to a checkbox, and therefore has a value of "TRUE" or "FALSE" based on whether or not the checkbox is checked.
- Cell C3 (.Cells(3, 3)) on each sheet is a date cell with the first of the month (i.e. on the Jan sheet, it is 1/1/2024).
Essentially I need the following conditions to be met for a month to be considered "available":
1. Cert Date (Column B) is not blank
2. Worksheet checkbox must be checked (aka Worksheet Cell A1 must be "TRUE")
3. Year (Cert Date) is before current year and transfer date (Column C) is blank or,
4. Year (Cert Date) is before current year and month(transfer date) is after worksheet month or,
5. Worksheet month is after the month (cert date) and the transfer date is blank
Hopefully that makes sense.
Here's a screenshot of my "Stats" sheet:
Here's a screenshot of one of the month's pages (showing A1 (currently hidden) and C3 = first day of the month. This is the same on every sheet):
I originally created this function back in the 2015-2018 time range, likely because I couldn't figure out the logic for a formula, if it's even possible with so many sheets and conditions. Hopefully one of you Excel Wizards can figure this one out for me.
Thank you and please let me know if you have any questions/need clarification!
I'm working on creating a new schedule for my job, to include a sheet for various stats. One of the stats I'm looking at is how many months a given worker was available based on their certification date ("cd") and whether or not they have transferred from/otherwise left the job ("td"). To do this, I've created a function MAvail (Months Available) to calculate how many months a worker was available based on a few conditions/logic. However, it seems my work network is not a fan of macros, so it only works as long as I don't close the file after creating the macro. I'm wondering if there is a formula that can help me calculate the months available.
Here is the code for the function I've created (to be clear, it does work, my work network is not cooperating):
VBA Code:
Function MAvail(cd As Range, td As Range) As Variant
Dim ma As Integer 'ma = month's available
For i = 1 To 12
If cd = "" Then 'If cert date (cd) is blank, then they are not available for any months.
ma = 0
ElseIf Year(td) < Year(Now()) Then 'Else if the transfer date (td) year is before the current year, then they are not available (Shouldn't happen ever, but just in case)
ma = 0
ElseIf Worksheets(i).Cells(1, 1).Value = True Then 'Else if A1=True (based on checkbox/linked cell on each month's tab) then,
If Year(cd) < Year(Now()) And td = "" Then 'If year of cert date is prior to current year and transfer date is blank then,
ma = ma + 1 'Add one available month
ElseIf Year(cd) < Year(Now()) And Month(td) >= Month(Worksheets(i).Cells(3, 3).Value) Then 'Else if cert date year is less than the current year AND transfer date month is AFTER (or is) the current sheet's month then,
ma = ma + 1 'Add one available month
ElseIf Month(Worksheets(i).Cells(3, 3).Value) >= Month(cd) And td = "" Then 'Else if the current sheet's month is after the cert date month AND transfer date is blank then,
ma = ma + 1 'Add one available month
End If
End If
Next i
MAvail = ma
End Function
I should explain a little of my set up:
- There are 13 tabs/worksheets in the workbook. 12 of those are schedules for each month. One of them is a "Stats" sheet.
- Cell A1 (.Cells(1, 1)) on each sheet is linked to a checkbox, and therefore has a value of "TRUE" or "FALSE" based on whether or not the checkbox is checked.
- Cell C3 (.Cells(3, 3)) on each sheet is a date cell with the first of the month (i.e. on the Jan sheet, it is 1/1/2024).
Essentially I need the following conditions to be met for a month to be considered "available":
1. Cert Date (Column B) is not blank
2. Worksheet checkbox must be checked (aka Worksheet Cell A1 must be "TRUE")
3. Year (Cert Date) is before current year and transfer date (Column C) is blank or,
4. Year (Cert Date) is before current year and month(transfer date) is after worksheet month or,
5. Worksheet month is after the month (cert date) and the transfer date is blank
Hopefully that makes sense.
Here's a screenshot of my "Stats" sheet:
Here's a screenshot of one of the month's pages (showing A1 (currently hidden) and C3 = first day of the month. This is the same on every sheet):
I originally created this function back in the 2015-2018 time range, likely because I couldn't figure out the logic for a formula, if it's even possible with so many sheets and conditions. Hopefully one of you Excel Wizards can figure this one out for me.
Thank you and please let me know if you have any questions/need clarification!