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:

From Date
To DateGaps
Employee 1
04/01/200110/01/2001
10/01/200111/1/2001
12/1/200103/01/2002Gap: 03/01/2002 to 02/12/2003
02/12/200305/12/2005
03/18/200301/01/2004
11/12/200401/31/2005
Employee 2
02/01/200605/01/2006Gap: 05/01/2006 to 06/26/2006
06/26/200609/30/2006Gap: 09/30/2006 to 01/02/2007
01/02/200703/16/2010Gap: 03/16/2010 to 05/03/2010
05/01/200704/03/2008
07/01/200702/22/2007
05/03/201007/31/2010
08/20/201005/30/2011
06/05/201106/06/2015
06/28/201511/20/2017
Employee 3
04/08/201307/27/2013
08/20/201311/24/2017
Employee 412/01/199508/31/2001Gap: 08/31/2001 to 10/30/2001
10/30/200107/17/2014
07/18/201411/26/2017

<tbody>
</tbody>

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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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