VBA loop to find gaps in an array of dates

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I am trying to move an array based formula from a worksheet into a for loop in VBA.
I currently have a list of from dates in column A and to dates in column B. These dates represent where an employee lived during a period of time. I have a formula in column C that shows the dates if there is a gap of 30 days or more where the employee doesn't have a location where he / she lived. Here is the formula:

{=IF(C2=MAX(C$2:C$99),"",IF(SUMPRODUCT((C2+30>=B$2:B$99)*(C2< C$2:C$99))=0,"Gap: " & TEXT(C2,"MM/DD/YYYY") & " to " & TEXT(MIN(IF(B$2:B$99>C2,B$2:B$99)),"MM/DD/YYYY"),""))}

This formula works perfectly in Excel. I am trying to move this to VBA because my database reports multiple employees on the same spreadsheet separated by empty cells in column B and C. For example I will have the dates employee a lived from rows 2 to 12. B from rows 14 to 35 and so on. Of course these vary based off how many locations they have lived. Below is an example of what I am looking for the script to do:

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]From Date
[/TD]
[TD]To Date[/TD]
[TD]Gaps[/TD]
[/TR]
[TR]
[TD]Employee 1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]04/01/2001[/TD]
[TD]10/01/2001
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]10/01/2001[/TD]
[TD]11/1/2001[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]12/1/2001[/TD]
[TD]03/01/2002[/TD]
[TD]Gap: 03/01/2002 to 02/12/2003[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]02/12/2003[/TD]
[TD]05/12/2005[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]03/18/2003[/TD]
[TD]01/01/2004[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]11/12/2004[/TD]
[TD]01/31/2005[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Employee 2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]02/01/2006[/TD]
[TD]05/01/2006[/TD]
[TD]Gap: 05/01/2006 to 06/26/2006[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]06/26/2006[/TD]
[TD]09/30/2006[/TD]
[TD]Gap: 09/30/2006 to 01/02/2007[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]01/02/2007[/TD]
[TD]03/16/2010[/TD]
[TD]Gap: 03/16/2010 to 05/03/2010[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]05/01/2007[/TD]
[TD]04/03/2008[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]07/01/2007[/TD]
[TD]02/22/2007[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]05/03/2010[/TD]
[TD]07/31/2010[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]08/20/2010[/TD]
[TD]05/30/2011
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]06/05/2011[/TD]
[TD]06/06/2015[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]06/28/2015[/TD]
[TD]11/20/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Employee 3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]04/08/2013[/TD]
[TD]07/27/2013[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]08/20/2013[/TD]
[TD]11/24/2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Employee 4[/TD]
[TD]12/01/1995[/TD]
[TD]08/31/2001[/TD]
[TD]Gap: 08/31/2001 to 10/30/2001[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]10/30/2001[/TD]
[TD]07/17/2014[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]07/18/2014[/TD]
[TD]11/26/2017[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

So I will have to have one big loop referencing the last row in the worksheet and then a smaller loop stopping during blank cells.
Help is greatly appreciated! Thank you in advance!
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You could still do it with worksheet formulas if you want. Here is your original formula (see note at the end of my post) adapted to work with a helper column, which could be hidden once populated.

Note that my dates in columns B:C are in d/mm/yyyy format.


Book1
ABCDE
1From DateTo DateGaps
2Employee 12 
31/04/20011/10/2001
41/10/20011/11/2001
51/12/20011/03/2002Gap: 03/01/2002 to 02/12/2003
612/02/200312/05/2005
718/03/20031/01/2004
812/11/200431/01/2005
9Employee 29
101/02/20061/05/2006Gap: 05/01/2006 to 06/26/2006
1126/06/200630/09/2006Gap: 09/30/2006 to 01/02/2007
122/01/200716/03/2010Gap: 03/16/2010 to 05/03/2010
131/05/20073/04/2008
141/07/200722/02/2007
153/05/201031/07/2010
1620/08/201030/05/2011
175/06/20116/06/2015
1828/06/201520/11/2017
19Employee 319
208/04/201327/07/2013
2120/08/201324/11/2017
2222
2323
24Employee 41/12/199531/08/2001Gap: 08/31/2001 to 10/30/2001
2530/10/200117/07/2014
2618/07/201426/11/2017
2727
2828
Gaps
Cell Formulas
RangeFormula
D2=IF(B2="",ROW(),"")
E2{=IF(D2="",IF(C2=MAX(INDEX(C:C,MAX(D1:D$2)+1):INDEX(C:C,MIN(D2:D$1000)-1)),"",IF(SUMPRODUCT((C2+30>=INDEX(B:B,MAX(D1:D$2)+1):INDEX(B:B,MIN(D2:D$1000)-1)) *(C2C:C,MAX(D1:D$2)+1):INDEX(C:C,MIN(D2:D$1000)-1)))=0,"Gap: " & TEXT(C2,"MM/DD/YYYY") & " to " & TEXT(MIN(IF(INDEX(B:B,MAX(D1:D$2)+1):INDEX(B:B,MIN(D2:D$1000)-1)>C2,INDEX(B:B,MAX(D1:D$2)+1):INDEX(B:B,MIN(D2:D$1000)-1))),"MM/DD/YYYY"),"")),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.



N.B.
If you try to post a formula that contains a < symbol followed immediately by a letter, the forum software misinterprets your formula and truncates it. To avoid that, add a space between those two characters. I have fixed your post above for that problem.,
 
Upvote 0
Solution
Hi Peter,
Sorry for the delayed response.

The idea to add a helper column was absolutely brilliant! This worked perfectly. I might have to use the helper column idea in other reports that I have as well.
Also read through your signature and will make sure my posts look nicer in the future.

Thank you so much for the help!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top