Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Compare shorter date range against longer date range

Hi Excel Gurus !

I am in need of a macro / formula which is able to find the missing date range(s).
Example:
John stayed in Apartment A&B for period : 1 February until 15 September.

Rental paid shown in records :
Apartment A rental period : 1 January until 28 February
Apartment B rental period : 1 April until 31 July
Apartment A rental again : 1 June until 31 August (overlaps)

The above start date - end date is keyed into cell A1:B3.

I need excel to show results :
1 March - 31 March
1 September - 15 September
which represent periods where no rental paid was shown in records.

My thousand thanks! in advance for any help on this.
SauQ
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Compare shorter date range against longer date range

SauQ wrote:

I am in need of a macro / formula which is able to find the missing date
range(s). Example:
John stayed in Apartment A&B for period : 1 February until 15 September.

Rental paid shown in records :
Apartment A rental period : 1 January until 28 February
Apartment B rental period : 1 April until 31 July
Apartment A rental again : 1 June until 31 August (overlaps)

The above start date - end date is keyed into cell A1:B3.

I need excel to show results :
1 March - 31 March
1 September - 15 September
which represent periods where no rental paid was shown in records.


Not tested very thoroughly, but works with the above ranges (and tested
with a *very* small set of date ranges outside the above).

Sub findMissingDates()
On Error GoTo exiter
sd1 = CDate(InputBox("Start date?", , Date))
ed1 = CDate(InputBox("End date?", , Date))
ReDim dates(sd1 To ed1) As Boolean
For L0 = 1 To 3
'find missing dates
sd = CDate(Cells(L0, 1).Value)
ed = CDate(Cells(L0, 2).Value)
If sd < sd1 Then sd = sd1
If ed ed1 Then ed = ed1
If (0 < sd) And (0 < ed) Then
For L1 = sd To ed
dates(L1) = True
Next
End If
Next
For L0 = sd1 To ed1
'report missing dates
If Not (dates(L0)) Then
For L1 = L0 + 1 To ed1
If dates(L1) Then Exit For
Next
While Len(ActiveCell.Formula)
ActiveCell.Offset(1, 0).Select
Wend
If ((L1) = ed1) And Not(dates(ed1)) Then
ActiveCell.Value = CDate(L0) & " to " & CDate(L1)
Exit Sub
Else
ActiveCell.Value = CDate(L0) & " to " & CDate(L1 - 1)
L0 = L1
End If
End If
Next
exiter:
End Sub

If the start & end dates for the overall period are specified somewhere on
the sheet, assign those values to sd1 and ed1 at the beginning of the sub
(and remove the two InputBox lines).

The locations for the individual rental dates are hardcoded in the first
For loop. (L0 is the rows.)

The missing dates are entered into whatever cell you have selected when
you run the sub. It will step down the sheet until it finds a blank cell
to work with. Where and how it reports the missing dates is handled in the
line starting with "ActiveCell.Value = CDate".

--
The idea is giving me brain cancer.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Compare shorter date range against longer date range

On Thursday, May 23, 2013 11:16:47 PM UTC+8, Auric__ wrote:
SauQ wrote:



I am in need of a macro / formula which is able to find the missing date


range(s). Example:


John stayed in Apartment A&B for period : 1 February until 15 September.




Rental paid shown in records :


Apartment A rental period : 1 January until 28 February


Apartment B rental period : 1 April until 31 July


Apartment A rental again : 1 June until 31 August (overlaps)




The above start date - end date is keyed into cell A1:B3.




I need excel to show results :


1 March - 31 March


1 September - 15 September


which represent periods where no rental paid was shown in records.




Not tested very thoroughly, but works with the above ranges (and tested

with a *very* small set of date ranges outside the above).



Sub findMissingDates()

On Error GoTo exiter

sd1 = CDate(InputBox("Start date?", , Date))

ed1 = CDate(InputBox("End date?", , Date))

ReDim dates(sd1 To ed1) As Boolean

For L0 = 1 To 3

'find missing dates

sd = CDate(Cells(L0, 1).Value)

ed = CDate(Cells(L0, 2).Value)

If sd < sd1 Then sd = sd1

If ed ed1 Then ed = ed1

If (0 < sd) And (0 < ed) Then

For L1 = sd To ed

dates(L1) = True

Next

End If

Next

For L0 = sd1 To ed1

'report missing dates

If Not (dates(L0)) Then

For L1 = L0 + 1 To ed1

If dates(L1) Then Exit For

Next

While Len(ActiveCell.Formula)

ActiveCell.Offset(1, 0).Select

Wend

If ((L1) = ed1) And Not(dates(ed1)) Then

ActiveCell.Value = CDate(L0) & " to " & CDate(L1)

Exit Sub

Else

ActiveCell.Value = CDate(L0) & " to " & CDate(L1 - 1)

L0 = L1

End If

End If

Next

exiter:

End Sub



If the start & end dates for the overall period are specified somewhere on

the sheet, assign those values to sd1 and ed1 at the beginning of the sub

(and remove the two InputBox lines).



The locations for the individual rental dates are hardcoded in the first

For loop. (L0 is the rows.)



The missing dates are entered into whatever cell you have selected when

you run the sub. It will step down the sheet until it finds a blank cell

to work with. Where and how it reports the missing dates is handled in the

line starting with "ActiveCell.Value = CDate".



--

The idea is giving me brain cancer.




On Thursday, May 23, 2013 11:16:47 PM UTC+8, Auric__ wrote:
SauQ wrote:



I am in need of a macro / formula which is able to find the missing date


range(s). Example:


John stayed in Apartment A&B for period : 1 February until 15 September.




Rental paid shown in records :


Apartment A rental period : 1 January until 28 February


Apartment B rental period : 1 April until 31 July


Apartment A rental again : 1 June until 31 August (overlaps)




The above start date - end date is keyed into cell A1:B3.




I need excel to show results :


1 March - 31 March


1 September - 15 September


which represent periods where no rental paid was shown in records.




Not tested very thoroughly, but works with the above ranges (and tested

with a *very* small set of date ranges outside the above).



Sub findMissingDates()

On Error GoTo exiter

sd1 = CDate(InputBox("Start date?", , Date))

ed1 = CDate(InputBox("End date?", , Date))

ReDim dates(sd1 To ed1) As Boolean

For L0 = 1 To 3

'find missing dates

sd = CDate(Cells(L0, 1).Value)

ed = CDate(Cells(L0, 2).Value)

If sd < sd1 Then sd = sd1

If ed ed1 Then ed = ed1

If (0 < sd) And (0 < ed) Then

For L1 = sd To ed

dates(L1) = True

Next

End If

Next

For L0 = sd1 To ed1

'report missing dates

If Not (dates(L0)) Then

For L1 = L0 + 1 To ed1

If dates(L1) Then Exit For

Next

While Len(ActiveCell.Formula)

ActiveCell.Offset(1, 0).Select

Wend

If ((L1) = ed1) And Not(dates(ed1)) Then

ActiveCell.Value = CDate(L0) & " to " & CDate(L1)

Exit Sub

Else

ActiveCell.Value = CDate(L0) & " to " & CDate(L1 - 1)

L0 = L1

End If

End If

Next

exiter:

End Sub



If the start & end dates for the overall period are specified somewhere on

the sheet, assign those values to sd1 and ed1 at the beginning of the sub

(and remove the two InputBox lines).



The locations for the individual rental dates are hardcoded in the first

For loop. (L0 is the rows.)



The missing dates are entered into whatever cell you have selected when

you run the sub. It will step down the sheet until it finds a blank cell

to work with. Where and how it reports the missing dates is handled in the

line starting with "ActiveCell.Value = CDate".



--

The idea is giving me brain cancer.



Thank you, THANK YOU , thank you very much Auric !!

It works like a charm. I have approx 3000 employees who have a rental record span between 6 months and 5 years to sort out. Some of them have as much as 5 apartments through out their employment.

Your VBA is truly a life savior for me.

Thanks again!
SAUQ
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
Compare an Array to a Date Range Mashuganah Excel Discussion (Misc queries) 9 April 2nd 10 07:35 PM
how to compare a date range another range tkraju via OfficeKB.com Excel Programming 3 April 22nd 09 02:31 AM
Rolling up the latest date, criteria of range to compare highest d JulieMK Excel Programming 0 April 10th 08 01:28 AM
copy date based on date -refer to date range mindpeace[_4_] Excel Programming 1 June 3rd 06 01:30 PM
Compare date of a transaction against a range of dates D Campbell Excel Worksheet Functions 1 August 7th 05 09:40 PM


All times are GMT +1. The time now is 10:12 PM.

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

About Us

"It's about Microsoft Excel"