returning a concatenated text list reflecting non-zero values

pingpong777

New Member
Joined
Apr 6, 2015
Messages
42
Hi. I have a data set that is annual values for various colors. For each of these colors, in any year the value will be 0 or greater. For each year, I would like to have a cell list all the colors that are non-zero for that year. For instance, for the table below (apologies for formatting), 2002 would read "rust, orange, navy, cyan", 2003 would read "rust, purple, pink", and so on. Is there an elegant way to do this in a single cell for each year, or am I stuck building a 2-3 step process?

20022003200420052006
rust11444
orange20010
purple07202
pink03010
navy60234
cyan10071

<tbody>
</tbody>
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe this macro does what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetColorsByYear()
  Dim Col As Long
  For Col = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(9, Col).Value = Application.Trim(Join(Application.Transpose(Evaluate("IF(" & Cells(2, Col).Resize(6).Address & "=0,"""",A2:A7)"))))
  Next
End Sub[/td]
[/tr]
[/table]

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (GetColorsByYear) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Hi,

If you have the TEXTJOIN function, available with an Office365 subscription, it should do the job rather easily.

I don't have that function, so this Concatenate method would work, but if you have a rather Long list of Colors, the formula can grow quite large.

This assumes you want the results in the Same Column for the Year, formula copied across:


Book1
ABCDEF
120022003200420052006
2rust11444
3orange20010
4purple07202
5pink03010
6navy60234
7cyan10071
8
9rust, orange, navy, cyanrust, purple, pinkrust, purple, navyrust, orange, pink, navy, cyanrust, purple, navy, cyan
Sheet581
Cell Formulas
RangeFormula
B9=IF(B2,$A2,"")&IF(B3,", "&$A3,"")&IF(B4,", "&$A4,"")&IF(B5,", "&$A5,"")&IF(B6,", "&$A6,"")&IF(B7,", "&$A7,"")
 
Upvote 0
you can also try PowerQuery

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    UnPivot = Table.UnpivotOtherColumns(Source, {"Color"}, "Attribute", "Value"),
    Filter = Table.SelectRows(UnPivot, each ([Value] <> 0)),
    Group = Table.Group(Filter, {"Attribute"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Color", each Table.Column([Count],"Color")),
    Extract = Table.TransformColumns(List, {"Color", each Text.Combine(List.Transform(_, Text.From), ", "), type text}),
    RemCols = Table.RemoveColumns(Extract,{"Count"})
in
    RemCols[/SIZE]

Color
2002
2003
2004
2005
2006
AttributeColor
rust
1​
1​
4​
4​
4​
2002rust, orange, navy, cyan
orange
2​
0​
0​
1​
0​
2003rust, purple, pink, black
purple
0​
7​
2​
0​
2​
2004rust, purple, navy
pink
0​
3​
0​
1​
0​
2005rust, orange, pink, navy, cyan
navy
6​
0​
2​
3​
4​
2006rust, purple, navy, cyan, black
cyan
1​
0​
0​
7​
1​
black
0​
3​
0​
0​
8​
 
Last edited:
Upvote 0
Oops, use this modified version of my formula instead, for in cases where the First color is 0 (so it won't produce a result with a leading comma space ", "):

Will post back.
 
Last edited:
Upvote 0
Sorry, got interrupted...

Here's the correct modified version:


Book1
ABCDEF
120022003200420052006
2rust11044
3orange20010
4purple07202
5pink03010
6navy60234
7cyan10071
8
9rust, orange, navy, cyanrust, purple, pinkpurple, navyrust, orange, pink, navy, cyanrust, purple, navy, cyan
Sheet581
Cell Formulas
RangeFormula
B9=SUBSTITUTE(TRIM(IF(B2,$A2&" ","")&IF(B3,$A3&" ","")&IF(B4,$A4&" ","")&IF(B5,$A5&" ","")&IF(B6,$A6&" ","")&IF(B7,$A7,""))," ",", ")
 
Upvote 0
actually, this doesn't quite work. if any year has a 0 value for rust, then the list starts out with a comma. any other suggestions?
 
Upvote 0
actually, this doesn't quite work. if any year has a 0 value for rust, then the list starts out with a comma. any other suggestions?

I am guessing you are not interested in the macro solution that I posted in Message #2 , correct?
 
Upvote 0

Forum statistics

Threads
1,214,285
Messages
6,118,673
Members
448,845
Latest member
MrEbzz

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