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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You are assuming only 30 days in a month?

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

Code:
Dim x As Long

x = 45

Call MsgBox(Int(x / 30) & " Month " & x - (Int(x / 30) * 30) & " days")
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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:
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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