Converting 1995 - 2001 field into 1995,1996,1997,1998,1999,2000,2001 etc

chrisjako

New Member
Joined
Apr 10, 2014
Messages
28
Can anyone help with getting a number range to convert to a sequential list of numbers?

I have columns with fields such as "1995 - 2001" or "2005 - 2018" which i want to convert into a sequence of numbers in the corresponding row former example:

1995,1996,1997,1998,1999,2000,2001

Thanks in advance
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This is a UDF (User Defined Function) in VBA ... Just copy the below code in VBA Module and assuming you have your data in cell A1 "1995 - 2001" then type in B1 the following formula =YearsRg(A1)

Rich (BB code):
Function YearsRg(txt As String) As String
'You can change the name of the function "YearsRg" to whatever you desire

For x = 0 To Right(txt, 4) - Left(txt, 4)
    YearsRg = YearsRg & Comma & Left(txt, 4) + x 'If you change the function name, you have to change it in this line too
    Comma = ","
Next x

End Function

Hope that helps & let me know if you have any question
 
Last edited:
Upvote 0
This is a UDF (User Defined Function) in VBA ... Just copy the below code in VBA Module and assuming you have your data in cell A1 "1995 - 2001" then type in B1 the following formula =YearsRg(A1)
Rich (BB code):
Function YearsRg(txt As String) As String
'You can change the name of the function "YearsRg" to whatever you desire

For x = 0 To Right(txt, 4) - Left(txt, 4)
    YearsRg = YearsRg & Comma & Left(txt, 4) + x 'If you change the function name, you have to change it in this line too
    Comma = ","
Next x

End Function
You can write your function as a one-liner (hence, no loop)...
Code:
[table="width: 500"]
[tr]
	[td]Function YearsRg(S As String) As String
  YearsRg = Join(Evaluate("TRANSPOSE(ROW(" & Replace(Replace(S, " ", ""), "-", ":") & "))"), ",")
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Thanks Rick for the one-liner code … I still don't fully understand the Evaluate function :)
 
Upvote 0
Glad we could help & thanks for reporting back
 
Upvote 0
You can write your function as a one-liner (hence, no loop)...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function YearsRg(S As String) As String
  YearsRg = Join(Evaluate("TRANSPOSE(ROW(" & Replace(Replace(S, " ", ""), "-", ":") & "))"), ",")
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Nice! :cool:

M.
 
Upvote 0
Sorry one last question, how would I convert it so the year range was something like 84 - 92 instead of 1984 - 1992

so my source field is 84 - 92 and my result field would be 1984,1985,1986,1987,1988,1989,1990,1991,1992
 
Upvote 0
What is the purpose of this ? Date of births ? I am asking because we have to know if 12 - 18 means 1912 - 1918 or 2012 - 2018 …. Can we take a cut-off year for example anything below 70 should be 1970 and 71+ should be 2071
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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