Days between two dates separated into days per month

wjohnson1976

New Member
Joined
Aug 14, 2010
Messages
11
Hello all,

I am trying to make an irrigation water demand worksheet for engineers and I am almost finished, but the date entry is perplexing me...

I want a user to be able to enter two dates then the days per month between the two populate a column as shown below. For example user enters "05/20/2010" and "09/14/2010"

Month Days
Jan 0
Feb 0
Mar 0
Apr 0
May 11
Jun 30
Jul 31
Aug 31
Sep 14
Oct 0
Nov 0
Dec 0

I tried using nested ifs with text and it was getting way out of hand for me. I realize the total number of days between can be easily displayed but the jump to separate months and let the other months=0 is out of my league!

Thanks in advance!

W. Johnson
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Excel Workbook
AB
1Start DateEnd Date
25/20/20109/14/2010
3
4MonthDays
5Jan0
6Feb0
7Mar0
8Apr0
9May11
10Jun30
11Jul31
12Aug31
13Sep14
14Oct0
15Nov0
16Dec0
...
Cell Formulas
RangeFormula
B5=MAX(0,MIN(DATE(2010,ROW(2:2),0),$B$2) - MAX(DATE(2010,ROW(1:1),0),$A$2))
B6=MAX(0,MIN(DATE(2010,ROW(3:3),0),$B$2) - MAX(DATE(2010,ROW(2:2),0),$A$2))
B7=MAX(0,MIN(DATE(2010,ROW(4:4),0),$B$2) - MAX(DATE(2010,ROW(3:3),0),$A$2))
B8=MAX(0,MIN(DATE(2010,ROW(5:5),0),$B$2) - MAX(DATE(2010,ROW(4:4),0),$A$2))
B9=MAX(0,MIN(DATE(2010,ROW(6:6),0),$B$2) - MAX(DATE(2010,ROW(5:5),0),$A$2))
B10=MAX(0,MIN(DATE(2010,ROW(7:7),0),$B$2) - MAX(DATE(2010,ROW(6:6),0),$A$2))
B11=MAX(0,MIN(DATE(2010,ROW(8:8),0),$B$2) - MAX(DATE(2010,ROW(7:7),0),$A$2))
B12=MAX(0,MIN(DATE(2010,ROW(9:9),0),$B$2) - MAX(DATE(2010,ROW(8:8),0),$A$2))
B13=MAX(0,MIN(DATE(2010,ROW(10:10),0),$B$2) - MAX(DATE(2010,ROW(9:9),0),$A$2))
B14=MAX(0,MIN(DATE(2010,ROW(11:11),0),$B$2) - MAX(DATE(2010,ROW(10:10),0),$A$2))
B15=MAX(0,MIN(DATE(2010,ROW(12:12),0),$B$2) - MAX(DATE(2010,ROW(11:11),0),$A$2))
B16=MAX(0,MIN(DATE(2010,ROW(13:13),0),$B$2) - MAX(DATE(2010,ROW(12:12),0),$A$2))
 
Upvote 0
Hi, Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Aug13
[COLOR="Navy"]Dim[/COLOR] stdt [COLOR="Navy"]As[/COLOR] Date, EdDt [COLOR="Navy"]As[/COLOR] Date, oDys [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c, m, mm
stdt = Range("C1")
EdDt = Range("D1")
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] oDys = stdt To EdDt
        [COLOR="Navy"]If[/COLOR] Month("1/" & Dn & "/2010") = Month(oDys) [COLOR="Navy"]Then[/COLOR]
            c = c + 1
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] oDys
Dn.Offset(, 1) = IIf(c > 0, c, 0)
c = 0
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Code:
[COLOR="RoyalBlue"][B]Row No [/B][/COLOR] [COLOR="RoyalBlue"][B]Column(A) [/B][/COLOR] [COLOR="RoyalBlue"][B]Column(B) [/B][/COLOR] [COLOR="RoyalBlue"][B]Column(C)  [/B][/COLOR] [COLOR="RoyalBlue"][B]Column(D)  [/B][/COLOR]
1.      Jan        17         15/01/2010  20/12/2010 
2.      Feb        28                                
3.      Mar        31                                
4.      Apr        30                                
5.      May        31                                
6.      Jun        30                                
7.      Jul        31                                
8.      Aug        31                                
9.      Sep        30                                
10.     Oct        31                                
11.     Nov        30                                
12.     Dec        20
Regards Mick





Regards Mick
 
Upvote 0
Thanks for the responses!

@ MickG, brilliant! I am quite new to macros and subs and all of this so thanks. Maybe a naive question but is there a way to force macros to run automatically? I really am aiming for something where adjustments to the dates will reflect instantly on the greater data calculations...but if not, you still have helped immensely so thank you.

@ AlphaFrog, I like the fact that there is not a macro to run after each update, however January simply does not work. Am I missing something?
 
Upvote 0
This is what I got for January. Same formulas as before.
Excel Workbook
AB
1Start DateEnd Date
21/21/20109/14/2010
3
4MonthDays
5Jan10
6Feb28
7Mar31
8Apr30
9May31
10Jun30
11Jul31
12Aug31
13Sep14
14Oct0
15Nov0
16Dec0
...
Cell Formulas
RangeFormula
B5=MAX(0,MIN(DATE(2010,ROW(2:2),0),$B$2) - MAX(DATE(2010,ROW(1:1),0),$A$2))


Could you better explain what doesn't work? Do you get an error? Is the Days wrong?
 
Upvote 0
Hi, Try this:-
This is a worksheet _Change Event, when you change the value in "D1" the code will run. (Change Target Address at top of code if Required)
Right click sheet Tab, Select "View Code", VB window appears.
Paste the Entire code into the Vb window.
Close Vb Window.
Change "D1" value , code runs.

Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_Change(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] stdt [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] EdDt [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] oDys [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] C [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]If[/COLOR] Target.address(0, 0) = "D1" [COLOR="Navy"]Then[/COLOR]
stdt = Range("C1")
EdDt = Range("D1")
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] oDys = stdt To EdDt
        [COLOR="Navy"]If[/COLOR] Month("1/" & Dn & "/2010") = Month(oDys) [COLOR="Navy"]Then[/COLOR]
            C = C + 1
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] oDys
Dn.Offset(, 1) = IIf(C > 0, C, 0)
C = 0
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
@ AlphaFrog, you are right...it does work as you said! Sorry, I think I pasted the code into previously formatted cells maybe. It was coming up zero in January before. Anyway, when I created a whole new sheet and tried, it worked fine. GREAT WORK. You rule.

@ MickG, yours works well too! Will definitely keep this for a future use in case I want any code to run when I change a specific cell. You rule as well.

As for which one I will use today? I don't know! :cool: but I wish I could give you both credit when I present my little program. Couldn't have done this without you.

Peace!
 
Upvote 0
@AlphaFrog
It's really great solution. But my problem is to handle more than one start day and more than end day correspondingly.

For Example:
StartDate EndDate
Jan 1, 2015 Jan 5, 2015
Jan 27, 2015 Feb 2, 2015
Feb 20, 2015 Mar 15, 2015
Mar 1, 2015 May 2, 2015
Mar 3, 2015 Mar 20, 2015

So I need one formula per month to calculate all the dates at once.
Jan?
Feb?
Mar?
Apr?
May?
Jun?
Jul?
Aug?
Sep?
Oct?
Nov?
Dec?

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>


I tried to convert your formula to array formula but I have problem with Min and Max. I cannot return Min and Max for each row in the array.

I appreciate any help
 
Upvote 0
I'm not the best at array formulas. There's probably a more elegant solution than this, but it works (limited to dates within the same year).
Excel Workbook
AB
1Start DateEnd Date
2Jan 1, 2015Jan 5, 2015
3Jan 27, 2015Feb 2, 2015
4Feb 20, 2015Mar 15, 2015
5Mar 1, 2015May 2, 2015
6Mar 3, 2015Mar 20, 2015
7
8MonthDays
9Jan10
10Feb11
11Mar64
12Apr30
13May2
14Jun0
Sheet
 
Upvote 0
@AlphaFrog, Thank you I really great help.
But I still thinking how to do the same using Max and Min function. Since I tried a lot to return the min/man for each row using one formula array, I wonder if that is possible?.
Thank you again
Best Regards
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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