Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
sforr
 
Posts: n/a
Default Number of semi-monthly periods between 2 dates

I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?
  #2   Report Post  
bj
 
Posts: n/a
Default

assuming that if the start date in the 15th only the second period would be
used and if the 15th was the last day no periods for that month would be
counted.
if there are two cells with Start-date and End-Date
try
= (year(End-Date)-Year(Start-date))*24+(month(Start-date)-month(End
Date))*2+if(day(start-date)<16,0,-1)+if(day(end-date)15,0,-1)

"sforr" wrote:

I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?

  #3   Report Post  
Biff
 
Posts: n/a
Default

Hi!

Try this.

A1 = start date
B1 = end date

Requires the Analysis ToolPak add-in be installed.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1)))=15))*2+OR(B1=EOMONT H(B1,0),DAY(B1)<15)-(DAY(A1)<=15)

This version does not require the ATP:

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1)))=15))*2+OR(B1=DATE(Y EAR(B1),MONTH(B1)+1,0),DAY(B1)<15)-(DAY(A1)<=15)

Biff

"sforr" wrote in message
...
I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?



  #4   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Mon, 13 Jun 2005 23:03:50 -0400, "Biff" wrote:

Hi!

Try this.

A1 = start date
B1 = end date

Requires the Analysis ToolPak add-in be installed.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1)))=15))*2+OR(B1=EOMONT H(B1,0),DAY(B1)<15)-(DAY(A1)<=15)


You are not counting ONLY full pay periods.

For example:

StartDate = 1/13/2005
End Date = 3/18/2005

Your formula(s) -- 5

Full Periods:

1/16 - 1/31
2/1 - 2/15
2/16 - 2/28
3/1 - 3/15


--ron
  #5   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Mon, 13 Jun 2005 11:42:03 -0700, "sforr"
wrote:

I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?


The issue, of course, is that you only want COMPLETE periods between the two
dates.

It's relatively easy to devise a UDF (user defined function) in VBA.

To enter this, <alt-F11 opens the VB editor. Ensure your project is
highlighted inthe project explorer window, then Insert/Module and paste the
code below into the window that opens.

To use the UDF, in some cell enter the formula:

=semimonthly(StartDate,EndDate)

where StartDate and EndDate refer to the cells where you have that information.

==================================
Function SemiMonthly(StartDate As Date, EndDate As Date) As Long
Dim FirstStartDate As Date
Dim LastEndDate As Date
Dim i As Long

If Day(StartDate) 1 And Day(StartDate) <= 16 Then
FirstStartDate = DateSerial(Year(StartDate), Month(StartDate), 16)
Else
FirstStartDate = StartDate - Day(StartDate) + 33 - Day(StartDate -
Day(StartDate) + 32)
End If

If Day(EndDate) < 15 Then LastEndDate = EndDate - Day(EndDate)
If Day(EndDate) = 15 Then LastEndDate = DateSerial(Year(EndDate),
Month(EndDate), 15)
If Month(EndDate + 1) < Month(EndDate) Then LastEndDate = EndDate

Debug.Print StartDate & " " & Format(FirstStartDate, "mm-dd-yyyy")
Debug.Print EndDate & " " & Format(LastEndDate, "mm-dd-yyyy")

For i = FirstStartDate To LastEndDate
If Day(i) = 1 Or Day(i) = 16 Then
SemiMonthly = SemiMonthly + 1
End If
Next i

End Function
==============================

If you want a worksheet formula approach, the function below mimics the UDF and
should give the same result.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(IF(AND(DAY(StartDate)1,DAY(
StartDate)<=16),DATE(YEAR(StartDate),MONTH(StartDa te),16),StartDate -
DAY(StartDate) + 33 - DAY(StartDate - DAY(StartDate) + 32))&":"&IF(
MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF(DAY(En dDate)
<15,EndDate-DAY(EndDate),DATE(YEAR(EndDate),MONTH(
EndDate),15))))))={1,16}))*(IF(AND(DAY(StartDate) 1,DAY(StartDate)
<=16),DATE(YEAR(StartDate),MONTH(StartDate),16),St artDate -
DAY(StartDate) + 33 - DAY(StartDate - DAY(StartDate) + 32))<IF(
MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF(DAY(
EndDate)<15,EndDate-DAY(EndDate),DATE(YEAR(EndDate),
MONTH(EndDate),15))))

HTH,

--ron


  #6   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Mon, 13 Jun 2005 11:42:03 -0700, "sforr"
wrote:

I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?


Small OOPS in the previously posted routines. The UDF should read:

===========================
Function SemiMonthly(StartDate As Date, EndDate As Date) As Long
Dim FirstStartDate As Date
Dim LastEndDate As Date
Dim i As Long

If Day(StartDate) 1 And Day(StartDate) <= 16 Then
FirstStartDate = DateSerial(Year(StartDate), Month(StartDate), 16)
Else
FirstStartDate = StartDate - Day(StartDate) + 33 - Day(StartDate -
Day(StartDate) + 32)
End If
If Day(StartDate) = 1 Then FirstStartDate = StartDate

If Day(EndDate) < 15 Then LastEndDate = EndDate - Day(EndDate)
If Day(EndDate) = 15 Then LastEndDate = DateSerial(Year(EndDate),
Month(EndDate), 15)
If Month(EndDate + 1) < Month(EndDate) Then LastEndDate = EndDate

Debug.Print StartDate & " " & Format(FirstStartDate, "mm-dd-yyyy")
Debug.Print EndDate & " " & Format(LastEndDate, "mm-dd-yyyy")

For i = FirstStartDate To LastEndDate
If Day(i) = 1 Or Day(i) = 16 Then
SemiMonthly = SemiMonthly + 1
End If
Next i

End Function
==================================

and the worksheet formula should be:

=SUMPRODUCT(--(DAY(ROW(INDIRECT(IF(DAY(StartDate)=1,
StartDate,IF(AND(DAY(StartDate)1,DAY(StartDate)<= 16),DATE(
YEAR(StartDate),MONTH(StartDate),16),StartDate - DAY(
StartDate) + 33 - DAY(StartDate - DAY(StartDate) + 32)))&":"&
IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF(
DAY(EndDate)<15,EndDate-DAY(EndDate),DATE(YEAR(
EndDate),MONTH(EndDate),15))))))={1,16}))*(IF(DAY(
StartDate)=1,StartDate,IF(AND(DAY(StartDate)1,DAY (
StartDate)<=16),DATE(YEAR(StartDate),MONTH(StartDa te),
16),StartDate - DAY(StartDate) + 33 - DAY(StartDate - DAY(
StartDate) + 32)))<IF(MONTH(EndDate+1)<MONTH(
EndDate),EndDate,IF(DAY(EndDate)<15,EndDate-DAY(
EndDate),DATE(YEAR(EndDate),MONTH(EndDate),15))))


--ron
  #7   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Mon, 13 Jun 2005 11:42:03 -0700, "sforr"
wrote:

I need to calculate the number of semi-monthly pay periods between 2 dates,
with the pay periods being the 15th of the month and the last day of the
month. I only want complete periods. Any ideas?


One other small modification: Remove the two lines in the UDF that begin with
Debug.Print

I also cleaned up some potential line wrap issues on this copy.

==============================
Function SemiMonthly(StartDate As Date, EndDate As Date) As Long
Dim FirstStartDate As Date
Dim LastEndDate As Date
Dim i As Long

If Day(StartDate) 1 And Day(StartDate) <= 16 Then
FirstStartDate = DateSerial(Year(StartDate), _
Month(StartDate), 16)
Else
FirstStartDate = StartDate - Day(StartDate) + _
33 - Day(StartDate - Day(StartDate) + 32)
End If
If Day(StartDate) = 1 Then FirstStartDate = StartDate

If Day(EndDate) < 15 Then LastEndDate = EndDate - Day(EndDate)
If Day(EndDate) = 15 Then LastEndDate = DateSerial _
(Year(EndDate), Month(EndDate), 15)
If Month(EndDate + 1) < Month(EndDate) Then LastEndDate = EndDate

For i = FirstStartDate To LastEndDate
If Day(i) = 1 Or Day(i) = 16 Then
SemiMonthly = SemiMonthly + 1
End If
Next i

End Function
=========================


--ron
  #8   Report Post  
Biff
 
Posts: n/a
Default

You are not counting ONLY full pay periods.

That's correct, I was only counting pay dates.

Biff

"Ron Rosenfeld" wrote in message
...
On Mon, 13 Jun 2005 23:03:50 -0400, "Biff" wrote:

Hi!

Try this.

A1 = start date
B1 = end date

Requires the Analysis ToolPak add-in be installed.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1)))=15))*2+OR(B1=EOMONT H(B1,0),DAY(B1)<15)-(DAY(A1)<=15)


You are not counting ONLY full pay periods.

For example:

StartDate = 1/13/2005
End Date = 3/18/2005

Your formula(s) -- 5

Full Periods:

1/16 - 1/31
2/1 - 2/15
2/16 - 2/28
3/1 - 3/15


--ron



  #9   Report Post  
Harlan Grove
 
Posts: n/a
Default

Biff wrote...
Try this.

A1 = start date
B1 = end date

....
This version does not require the ATP:

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1)))=15))*2
+OR(B1=DATE(YEAR(B1),MONTH(B1)+1,0),DAY(B1)<15)-(DAY(A1)<=15)

....

Why not brute force with a slight twist?

=SUMPRODUCT(--(DAY(ROW(INDIRECT(A1&":"&B1))+{0,1})={15,1}))

  #10   Report Post  
Harlan Grove
 
Posts: n/a
Default

Ron Rosenfeld wrote...
....
If you want a worksheet formula approach, the function below mimics the UDF and
should give the same result.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(IF(AND(DAY(StartDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(St artDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
&":"&IF(MONTH(EndDate+1)<MONTH(EndDate),EndDat e,
IF(DAY(EndDate)<15,EndDate-DAY(EndDate),DATE(YEAR(EndDate),
MONTH(EndDate),15))))))={1,16}))*(IF(AND(DAY(Star tDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(St artDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
<IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF(D AY(EndDate)<15,
EndDate-DAY(EndDate),DATE(YEAR(EndDate),MONTH(EndDate),15) )))


Ugh!

If one can live with an array formula, why not

=INT((MAX(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDat e))+1)={1,16},
ROW(INDIRECT(StartDate&":"&EndDate))))
-MIN(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDate)))={ 1,16},
ROW(INDIRECT(StartDate&":"&EndDate)))))/15)

?



  #11   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On 14 Jun 2005 12:43:51 -0700, "Harlan Grove" wrote:

Ron Rosenfeld wrote...
...
If you want a worksheet formula approach, the function below mimics the UDF and
should give the same result.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(IF(AND(DAY(StartDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(S tartDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
&":"&IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate ,
IF(DAY(EndDate)<15,EndDate-DAY(EndDate),DATE(YEAR(EndDate),
MONTH(EndDate),15))))))={1,16}))*(IF(AND(DAY(Sta rtDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(S tartDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
<IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF( DAY(EndDate)<15,
EndDate-DAY(EndDate),DATE(YEAR(EndDate),MONTH(EndDate),15) )))


Ugh!

If one can live with an array formula, why not

=INT((MAX(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDa te))+1)={1,16},
ROW(INDIRECT(StartDate&":"&EndDate))))
-MIN(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDate)))={ 1,16},
ROW(INDIRECT(StartDate&":"&EndDate)))))/15)

?


Oh I agree with "Ugh!" and, as you know, I don't mind array formulas. Although
I'm happy with my UDF.

What I did was translate my UDF algorithm into worksheet code. Shorter
worksheet code would be better. But yours gives an incorrect result for, among
other examples:

StartDate: 3 Jan 2005
EndDate: 15 Mar 2005

Your formula gives a result of '3'. I believe correct answer is '4'.

16 Jan -- 31 Jan
1 Feb -- 15 Feb
16 Feb -- 28 Feb
1 Mar -- 15 Mar

By the way, the code in the message of mine you quoted also gives incorrect
results in some instances (although not this instance); and was corrected later
in the thread.

Best,
--ron
  #12   Report Post  
Biff
 
Posts: n/a
Default

Using Ron's sample date range:

StartDate = 1/13/2005
End Date = 3/18/2005

Formula returns 3.

Biff

"Harlan Grove" wrote in message
oups.com...
Ron Rosenfeld wrote...
...
If you want a worksheet formula approach, the function below mimics the
UDF and
should give the same result.

=SUMPRODUCT(--(DAY(ROW(INDIRECT(IF(AND(DAY(StartDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(S tartDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
&":"&IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate ,
IF(DAY(EndDate)<15,EndDate-DAY(EndDate),DATE(YEAR(EndDate),
MONTH(EndDate),15))))))={1,16}))*(IF(AND(DAY(Sta rtDate)1,
DAY(StartDate)<=16),DATE(YEAR(StartDate),MONTH(S tartDate),16),
StartDate-DAY(StartDate)+33-DAY(StartDate-DAY(StartDate)+32))
<IF(MONTH(EndDate+1)<MONTH(EndDate),EndDate,IF( DAY(EndDate)<15,
EndDate-DAY(EndDate),DATE(YEAR(EndDate),MONTH(EndDate),15) )))


Ugh!

If one can live with an array formula, why not

=INT((MAX(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDat e))+1)={1,16},
ROW(INDIRECT(StartDate&":"&EndDate))))
-MIN(IF(DAY(ROW(INDIRECT(StartDate&":"&EndDate)))={ 1,16},
ROW(INDIRECT(StartDate&":"&EndDate)))))/15)

?



  #13   Report Post  
Harlan Grove
 
Posts: n/a
Default

Ron Rosenfeld wrote...
What I did was translate my UDF algorithm into worksheet code. Shorter
worksheet code would be better. But yours gives an incorrect result for, among
other examples:

StartDate: 3 Jan 2005
EndDate: 15 Mar 2005

Your formula gives a result of '3'. I believe correct answer is '4'.

....

You're right. I didn't consider short periods at the beginning of the
year, in which February would screw up dividing days by 15 to get half
month counts.

An opportunity to simplify the formula. Now not even an array formula.

=SUMPRODUCT((DAY(ROW(INDIRECT(StartDate&":"&EndDat e)))={1,16,16,16,16})
*(DAY(ROW(INDIRECT(StartDate&":"&EndDate))+{0,13,1 4,15,16})=1)
*(ROW(INDIRECT(StartDate&":"&EndDate))+{14,12,13,1 4,15}<=EndDate))

  #14   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On 15 Jun 2005 11:46:12 -0700, "Harlan Grove" wrote:

An opportunity to simplify the formula. Now not even an array formula.

=SUMPRODUCT((DAY(ROW(INDIRECT(StartDate&":"&EndDa te)))={1,16,16,16,16})
*(DAY(ROW(INDIRECT(StartDate&":"&EndDate))+{0,13, 14,15,16})=1)
*(ROW(INDIRECT(StartDate&":"&EndDate))+{14,12,13, 14,15}<=EndDate))


Very nice!


--ron
  #15   Report Post  
Biff
 
Posts: n/a
Default

Isn't it amazing how some threads deal with "complicated" solutions and
generate some really top notch contributions yet the OP is nowhere in sight!

Biff

"Ron Rosenfeld" wrote in message
...
On 15 Jun 2005 11:46:12 -0700, "Harlan Grove" wrote:

An opportunity to simplify the formula. Now not even an array formula.

=SUMPRODUCT((DAY(ROW(INDIRECT(StartDate&":"&EndD ate)))={1,16,16,16,16})
*(DAY(ROW(INDIRECT(StartDate&":"&EndDate))+{0,13 ,14,15,16})=1)
*(ROW(INDIRECT(StartDate&":"&EndDate))+{14,12,13 ,14,15}<=EndDate))


Very nice!


--ron





  #16   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Wed, 15 Jun 2005 22:11:55 -0400, "Biff" wrote:

Isn't it amazing how some threads deal with "complicated" solutions and
generate some really top notch contributions yet the OP is nowhere in sight!


Yes it is. But an important reason for my participation here is to advance my
own state of knowledge; so these threads are still valuable to me.


--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formating Dates for production schedule dpl7579 Excel Discussion (Misc queries) 1 January 11th 05 08:43 PM
Total number of each month in a column of dates GeorgeF. Excel Worksheet Functions 1 November 19th 04 06:10 PM
Number format-numbers appear as dates in Excel Tony Moffat Excel Worksheet Functions 2 November 11th 04 12:40 AM
Calculate the number of workdays between 2 dates Tegid77 Excel Worksheet Functions 2 November 4th 04 11:09 PM
Calculate the number of workdays between 2 dates Tegid77 Excel Worksheet Functions 1 November 4th 04 07:27 PM


All times are GMT +1. The time now is 08:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"