# Get Year And Months From Months

I neee a rotuine that will be passed the number of months and needs to return a string var of the number of years and a string var of the number of months.

Thought this was going to be easy but rounding is getting in the way.  On a numbe of months like 23 I am getting 2 year -1 months.

Here's what I have so far, which doesn't work.

``````Public Sub convertNumMonthsToYearsandMonths(passedNumberOfMonths As Long, _
returnYearStr As String, _
returnMonthsStr As String)
'round

Dim wkYears As Double
Dim wkYearsInt As Long
Dim wkMonths As Long
'
If passedNumberOfMonths < 12 Then
wkYears = 0
Else
wkYears = Round(passedNumberOfMonths / 12, 2)
End If
wkYearsInt = CInt(wkYears)
'
wkMonths = passedNumberOfMonths - (wkYearsInt * 12)
'
returnYearStr = Trim(Str(wkYearsInt))
returnMonthsStr = Trim(Str(wkMonths))
'
End Sub
``````

Hwo can I get this to work?
LVL 1
###### Who is Participating?

x

Commented:
Seems like you can do this easily without a function:

``````iYears = Int(totalMonths / 12)
iMonths = ((totalMonths / 12) - iYears) * 12
``````

Ron
0

Commented:
And there is maintain your data types:

``````Dim passedNumberOfMonths As Long
Dim returnYearStr As String
Dim returnMonthsStr As String

passedNumberOfMonths  = 23

returnYearStr =  Str(passedNumberOfMonths  \ 12)
returnMonthsStr  = Str(passedNumberOfMonths   - ((passedNumberOfMonths   \12)*12))
``````

Proof of Concept:

? 23 \ 12
1

? 23 - ((23 \12)*12)
11

or

``````Public Sub convertNumMonthsToYearsandMonths(passedNumberOfMonths As Long, _
ByRef  returnYearStr As As Variant, _
ByRef returnMonthsStr AsAs Variant)

returnYearStr = Trim(Str(passedNumberOfMonths  \ 12))
returnMonthsStr = Trim(Str(passedNumberOfMonths   - ((passedNumberOfMonths   \12)*12)))

End Sub
``````
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.