Join months name based on consecutive range and separate as well in specified format

hananak

Board Regular
Joined
Feb 10, 2022
Messages
114
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Dear All,

I need your help!

I have column A in which I have months and column B which has status, it will have "Y" or will be blank. I want a formula in D6, where based on "Y" in column B, it combines the month name.
1) If the range is consecutive then the formula should give result as the first month of the consecutive range and the last month and hyphen in between (Apr - Jun) and if it is not consecutive then just the month name separated by a comma (Nov). Please see the picture to better understand the output I would like to have.

I am using Office 365.

Please see the attached picture.
 

Attachments

  • 1.jpg
    1.jpg
    27.7 KB · Views: 16

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Is this what you mean?

24 12 30.xlsm
ABCD
1
2AprY
3MayY
4JunY
5Jul
6AugApr-Jun, Nov, Jan-Mar
7Sep
8Oct
9NovY
10Dec
11JanY
12FebY
13MarY
14
Months
Cell Formulas
RangeFormula
D6D6=SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y",A2:A13," "))))))," ","-"),"x"," "))," ",", ")
 
Upvote 0
Is this what you mean?

24 12 30.xlsm
ABCD
1
2AprY
3MayY
4JunY
5Jul
6AugApr-Jun, Nov, Jan-Mar
7Sep
8Oct
9NovY
10Dec
11JanY
12FebY
13MarY
14
Months
Cell Formulas
RangeFormula
D6D6=SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y",A2:A13," "))))))," ","-"),"x"," "))," ",", ")
Hi Peter,

Thanks! the formula is working but a slight adjustment is needed. Please see the attached picture. When the consecutive list has only two values, then it should be (Sep - Oct), currently it is showing SepOct.
Your help in sorting out this will be much appreciated.
 

Attachments

  • 1.jpg
    1.jpg
    34.3 KB · Views: 8
Upvote 0
When the consecutive list has only two values, then it should be (Sep - Oct),
:oops: Oops, yes I missed that circumstance. See if this fixes it without ruining another combination. :)

24 12 30.xlsm
ABCD
1
2AprY
3MayY
4JunY
5Jul
6AugApr-Jun, Sep-Oct, Dec-Jan, Mar
7SepY
8OctY
9Nov
10DecY
11JanY
12Feb
13MarY
Months (2)
Cell Formulas
RangeFormula
D6D6=SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y"," "&A2:A13," "))))))," ","-"),"x"," "))," ",", ")
 
Upvote 0
:oops: Oops, yes I missed that circumstance. See if this fixes it without ruining another combination. :)

24 12 30.xlsm
ABCD
1
2AprY
3MayY
4JunY
5Jul
6AugApr-Jun, Sep-Oct, Dec-Jan, Mar
7SepY
8OctY
9Nov
10DecY
11JanY
12Feb
13MarY
Months (2)
Cell Formulas
RangeFormula
D6D6=SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y"," "&A2:A13," "))))))," ","-"),"x"," "))," ",", ")
Hi Peter, the formula is now working. There is one thing, that I am trying to fix but it seems like it's not that simple. Currently the formula outputs Apr-Jun, I would like to change it to Apr - Jun. So basically a space before the hyphen and a space after the hyphen --> Dec - Jan. Can this be fixed?
Sorry, previously I overlooked this part.

Your help would be appreciated.
 
Upvote 0
There is one thing, that I am trying to fix but it seems like it's not that simple. Currently the formula outputs Apr-Jun, I would like to change it to Apr - Jun.
Just add one extra SUBSTITUTE
=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y"," "&A2:A13," "))))))," ","-"),"x"," "))," ",", "),"-"," - ")
 
Upvote 0
Solution
Just add one extra SUBSTITUTE
=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(CONCAT(IF(B2:B13="","x",IF(B2:B13="Y",IF(B1:B12<>"Y",A2:A13,IF(B3:B14<>"Y"," "&A2:A13," "))))))," ","-"),"x"," "))," ",", "),"-"," - ")
Thank you very much Peter for your quick response and resolving my query. The formula works perfectly, as I wanted. An interesting function, I will learn about this.
 
Upvote 0

Forum statistics

Threads
1,225,197
Messages
6,183,499
Members
453,165
Latest member
kuldeep08126

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