Is Year in YearString

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Just wondering th best way to see if string "1965" is within string "1944-1983".
Would it be better to convert to Date variables ?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Just wondering th best way to see if string "1965" is within string "1944-1983".
Would it be better to convert to Date variables ?

If you are willing to consider a UDF (user defined function)...
Code:
Function IsYearIn(TheYear As Variant, TheRange As Variant) As Boolean
  Dim Parts() As String
  Parts = Split(TheRange, "-")
  IsYearIn = Parts(0) <= TheYear And TheYear <= Parts(1)
End Function
 
Upvote 0
If you are willing to consider a UDF (user defined function)...
Code:
Function IsYearIn(TheYear As Variant, TheRange As Variant) As Boolean
  Dim Parts() As String
  Parts = Split(TheRange, "-")
  IsYearIn = Parts(0) <= TheYear And TheYear <= Parts(1)
End Function
And here is an Excel formula solution (assuming cell A1 contains the year being tested and cell A2 contains the date range text string)...

=AND((0+LEFT(A2,FIND("-",A2)-1))<=A1,A1<=(0+MID(A2,FIND("-",A2)+1,4)))
 
Upvote 0
Thanks very much Rick, it's doing the job perfectly. I wondered about a Date variable
being 'more correct' but I'm only using YYYY and the various other date parameters may be overkill.
 
Upvote 0
One way:

=SUMPRODUCT(--(ROW(INDIRECT(SUBSTITUTE(A2,"-",":")))=A1))>0
 
Upvote 0
There's no way to use a worksheet formula in VBA is there ?
 
Upvote 0
Nothing specific Rick, just to try out/test those from you and Phuoc above. Others turn up from time to time too.
 
Upvote 0
Nothing specific Rick, just to try out/test those from you and Phuoc above. Others turn up from time to time too.
As I indicated, it depends on the formula you want to implement... there is no "general" method that works for all formulas... like any other piece of VB code, the code has to be designed to respond to the exact situation it is meant for.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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