Extract the Month/s from a given text value

ExcelNewbie2020

Active Member
Joined
Dec 3, 2020
Messages
354
Office Version
  1. 365
Platform
  1. Windows
I would like to extract the months per year from the given values in column A with different kind of scenarios.

Book1
ABC
2GIVENEXPECTED RESULT
3YYYYMM20202021
4202001JANUARY
5202101JANUARY
6202001 & 202003JANUARY & MARCH
7202101, 202102, 202103JANUARY, FEBRUARY, MARCH
8202101 - 202110JANUARY - OCTOBER
9202001, 202004 - 202005 & 202012JANUARY, APRIL - MAY & DECEMBER
Sheet2
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Are you able to use vba code? If so, I think some things need clarification:
- when months are consecutive (04 - 05) can't B9 above be JANUARY, APRIL, MAY, DECEMBER ? Easier to use one format instead of dashes and ampersands as well.
- what if two values are 202004 - 202006? Is that [ April, June ] (I would hope) or [ April, May, June ] ? Or maybe that will never be the case?
 
Upvote 0
Hello, the following could work for the example provided but of course things might be a little bit more complicated within you data:

Excel Formula:
=LET(
y,B$3,
s,$A4,
d,REGEXEXTRACT(s,"\d{6}",1),
m,UPPER(TEXT(DATE(LEFT(d,4),RIGHT(d,2),1),"mmmm")),
j,IFERROR(REGEXEXTRACT(s,"\W{2,3}",1),""),
IF(ISNUMBER(SEARCH(y,s)),TEXTJOIN(j,,m),""))
 
Upvote 0
Another possibility, based on your sample data:
Excel Formula:
=LET(
    data, SUBSTITUTE(A4:A9,","," ,"),
    n, SEQUENCE(,MAX(LEN(data)-LEN(SUBSTITUTE(data," ",)))+1),
    a, IFERROR(TEXTBEFORE(TEXTAFTER(" "&data&" "," ",n)," "),""),
    b, IFERROR(UPPER(TEXT(RIGHT(a,2)*29,"mmmm")),a),
    m, SUBSTITUTE(BYROW(b,LAMBDA(r,TEXTJOIN(" ",1,r)))," ,",","),
    y, VALUE(LEFT(TAKE(a,,1),4)),
    DROP(PIVOTBY(SEQUENCE(ROWS(data)),y,m,SINGLE,0,0,,0),,1)
)
Please note, this assumes your actual data won't ever contain multiple years within a single delimited string.
 
Upvote 0
One more option:
Book1
ABC
1
2GIVENEXPECTED RESULT
3YYYYMM20202021
4202001JANUARY
5202101 JANUARY
6202001 & 202003JANUARY & MARCH
7202101, 202102, 202103 JANUARY , FEBRUARY , MARCH
8202101 - 202110 JANUARY - OCTOBER
9202001, 202004 - 202005 & 202012JANUARY , APRIL - MAY & DECEMBER
10202101 - 202110 JANUARY - OCTOBER
Sheet3
Cell Formulas
RangeFormula
B4:C10B4=LET(a,A4, d,{"&",",","-"},m,MID(a,SEQUENCE(LEN(a)),1), delim,IFERROR(TOCOL(IFS(ISNUMBER(XMATCH(m,d)),m),2),""), t,TRIM(TEXTSPLIT(a,d)),MAP($B$3:$C$3,LAMBDA(x,TEXTJOIN(" "&delim&" ",,FILTER(UPPER(TEXT(RIGHT(t,2)&"/1900","mmmm")),--LEFT(t,4)=x,"")))) )
Dynamic array formulas.
 
Upvote 0
I would like to extract the months per year from the given values in column A with different kind of scenarios.

Book1
ABC
2GIVENEXPECTED RESULT
3YYYYMM20202021
4202001JANUARY
5202101JANUARY
6202001 & 202003JANUARY & MARCH
7202101, 202102, 202103JANUARY, FEBRUARY, MARCH
8202101 - 202110JANUARY - OCTOBER
9202001, 202004 - 202005 & 202012JANUARY, APRIL - MAY & DECEMBER
Sheet2

Another option is to call a UDF placed in a main code module.

An array is returned by the function which spills out into the adjacent cells as appropriate.

This also assumes that your actual data won't ever contain multiple years within a single delimited string.

Place this function call in cell B3 and change the range as appropriate.

It places the column headings (years) as appropriate.

Excel Formula:
=fncExtractMonths(A4:A9)

VBA Code:
Public Function fncExtractMonths(rng As Range)
Dim arr() As Variant
Dim i As Integer
Dim arrYears() As Variant
Dim varYear As Variant
Dim m As Integer
Dim res() As Variant
Dim y As Integer
  
  arrYears = Evaluate("SORT(UNIQUE(LEFT(WRAPCOLS(" & rng.Address & ",20),4)))")

  arr = rng

  ReDim res(1 To UBound(arr) + 1, 1 To UBound(arrYears))
  
  For Each varYear In arrYears
  
    y = y + 1
    
    res(1, y) = varYear
      
    For i = 1 To UBound(arr)
      res(i + 1, y) = ""
      For m = 1 To 12
        If InStr(1, arr(i, 1), varYear & Right("0" & m, 2), vbTextCompare) > 0 Then
          arr(i, 1) = Replace(arr(i, 1), varYear & Right("0" & m, 2), Format("01/" & Right("0" & m, 2) & "/" & varYear, "MMMM"))
          res(i + 1, y) = arr(i, 1)
        End If
      Next m
    Next i
  
  Next varYear
    
  fncExtractMonths = res

End Function
 
Upvote 0

Forum statistics

Threads
1,226,463
Messages
6,191,181
Members
453,645
Latest member
BOUCHOUATA

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