Days to days and months

MartinCarlsson

Board Regular
Joined
Mar 8, 2012
Messages
59
Hi
I have x number of days, and I need it to make it to days and months.

Example.
x = 45

days -> 15
months -> 1

I know there is more than one way and Im looking for the best way to do it for my program.
Any suggestions?

//Martin
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

gaj104

Well-known Member
Joined
Nov 9, 2002
Messages
864
You are assuming only 30 days in a month?

If so, then =+FLOOR(A1/30,1)&" Month "&+A1-(FLOOR(A1/30,1)*30)&" days"
 

gaj104

Well-known Member
Joined
Nov 9, 2002
Messages
864
Try

Code:
Dim x As Long

x = 45

Call MsgBox(Int(x / 30) & " Month " & x - (Int(x / 30) * 30) & " days")
 

hurgadion

Active Member
Joined
Mar 19, 2010
Messages
426

ADVERTISEMENT

Hi,
I suggest more precise Solution, In A3 there is an array formula of the form (a number of Months):
Code:
=MAX(IF(DATE(YEAR(TODAY()),MONTH(TODAY())+ROW(A1:A1000),DAY(TODAY()))<=TODAY()+A1,ROW(A1:A1000),""))
and in B3 there is a formula (a remaining part of Minutes):
Code:
=DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())+A1)-DATE(YEAR(TODAY()),MONTH(TODAY())+A3;DAY(TODAY()))
Best regards.
 

MartinCarlsson

Board Regular
Joined
Mar 8, 2012
Messages
59
Try

Code:
Dim x As Long

x = 45

Call MsgBox(Int(x / 30) & " Month " & x - (Int(x / 30) * 30) & " days")


Thanks
I need to make the .. number of month, even if I have 1,000 days.

And if I exchange 30 for 365.2 / 12, it returns days like: 14,5666666666667

Code:
Sub heya()
    Dim x As Long
    Dim daysInMonths As Variant
    
    daysInMonths = 365.2 / 12
    x = 45
    
    Debug.Print (Int(x / daysInMonths) & " Month " & x - (Int(x / daysInMonths) * daysInMonths) & " days")


End Sub
Immediate:
1 Month 14,5666666666667 days




Hi,
I suggest more precise Solution, In A3 there is an array formula of the form (a number of Months):
Code:
=MAX(IF(DATE(YEAR(TODAY()),MONTH(TODAY())+ROW(A1:A1000),DAY(TODAY()))<=TODAY()+A1,ROW(A1:A1000),""))
and in B3 there is a formula (a remaining part of Minutes):
Code:
=DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())+A1)-DATE(YEAR(TODAY()),MONTH(TODAY())+A3;DAY(TODAY()))
Best regards.

Thanks.
Can you show it in VBA?
 

hurgadion

Active Member
Joined
Mar 19, 2010
Messages
426

ADVERTISEMENT

Try maybe using the macro of the form:
Code:
Sub lala()
Dim i&, z&, x&, y&

x = 45
Cells(1, 4).Formula = "=TODAY()"
y = Cells(1, 4).Value

For i = 1 To 1000
  If DateSerial(Year(y), Month(y) + i, Day(y)) <= DateSerial(Year(y), Month(y), Day(y) + x) Then
    z = i
  Else
    Exit For
  End If
Next i

Cells(1, 3).Value = z
Cells(1, 4).Value = y + x - DateSerial(Year(y), Month(y) + z, Day(y))
Cells(1, 4).NumberFormat = "General"
End Sub
Best regards.
 
Last edited:

MartinCarlsson

Board Regular
Joined
Mar 8, 2012
Messages
59
Try maybe using the macro of the form:
Code:
Sub lala()
Dim i&, z&, x&, y&

x = 45
Cells(1, 4).Formula = "=TODAY()"
y = Cells(1, 4).Value

For i = 1 To 1000
  If DateSerial(Year(y), Month(y) + i, Day(y)) <= DateSerial(Year(y), Month(y), Day(y) + x) Then
    z = i
  Else
    Exit For
  End If
Next i

Cells(1, 3).Value = z
Cells(1, 4).Value = y + x - DateSerial(Year(y), Month(y) + z, Day(y))
Cells(1, 4).NumberFormat = "General"
End Sub
Best regards.

Thanks!

Your code is a bit confusing.
Why do you use "=TODAY()" instead og Now()
Why do you loop?
Why do you end the loop with an if statement instead of the usual way?
 

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,194
Try this In Immediate Window
Code:
Sub Test_Immediate_Window()
Dim IntDays As Long
IntDays = 100000000
Debug.Print Evaluate("=+FLOOR( " & IntDays & "/30,1)& "" Month "" & +" & IntDays & "-(FLOOR( " & IntDays & "/30,1)*30)&"" days""")
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,669
Messages
5,597,470
Members
414,145
Latest member
lonnie451

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
Top