concatenate only non zero values

NapervilleMike

New Member
Joined
Aug 1, 2012
Messages
19
So here is what I have:

Substancequantity
Water98
Sodium.5
Potassium1
Calcium.5
Magnesium0

<tbody>
</tbody>

What I need is a formula that will concatenate only the non-zero's and the substance associated with them. For example this out put should say: Water, 98; Sodium .5; Potassium, 1; Calcium .5

If this seems pointless, the reason I need a methodology because I actually have about 50 substances and there are about 30 "quantity" columns and I would like a nice way to automate this and apply it to other similar applications.

Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
function argument: enter your whole range, ie A2:B6

Code:
Public Function ConcSub(aRange As Range) As String


Dim vArr
vArr = aRange


Dim x As Long, p As Long, temp As String


For x = LBound(vArr) To UBound(vArr)
    If Not vArr(x, 2) = 0 Then
        temp = temp & vArr(x, 1) & ", " & vArr(x, 2) & "; "
    End If
Next


ConcSub = Left(temp, Len(temp) - 2)




End Function
 
Upvote 0
Here is a different way to write the UDF (user defined function)...this one uses no loops. Note I called my function JoinSubs.

Code:
Function JoinSubs(Rng As Range) As String
  Dim X As Long, LastRow As Long, Txt As String
  LastRow = Cells(Rows.Count, Rng.Column).End(xlUp).Row
  JoinSubs = Trim(Join(Application.Transpose(Evaluate("IF(" & Rng.Columns(2).Address & "=0,""""," & _
                  Rng.Columns(1).Address & "&"", ""&" & Rng.Columns(2).Address & ")")), "; "))
  If Right(JoinSubs, 1) = ";" Then JoinSubs = Left(JoinSubs, Len(JoinSubs) - 1)
End Function

Just for completeness...

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, 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. You can now use JoinSubs just like it was a built-in Excel function. For example,


=JoinSubs(A2:B6)


where A2:B6 is the range containing the Substances and Quantities. 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
Thanks guys, one more question.

My example range was A2:B6 but for each additional quantity column, how do I define that? For example, quantity for my first sample is column B, but the second is column C all the way to column n. Can I use this function for all of these?
 
Upvote 0
Thanks guys, one more question.

My example range was A2:B6 but for each additional quantity column, how do I define that? For example, quantity for my first sample is column B, but the second is column C all the way to column n. Can I use this function for all of these?

Ah, I see... the Substance column doesn't change and the Quality columns are stacked next to each other. Okay, let's change the single argument for the function to two arguments... the Subtance range and the the Quality range. Here is my UDF modified for this...

Code:
Function JoinSubs(SubstanceRange As Range, QualityRange As Range) As String
  Dim X As Long, LastRow As Long, Txt As String
  LastRow = Cells(Rows.Count, SubstanceRange.Column).End(xlUp).Row
  JoinSubs = Trim(Join(Application.Transpose(Evaluate("IF(" & QualityRange.Address & "=0,""""," & _
                  SubstanceRange.Address & "&"", ""&" & QualityRange.Address & ")")), "; "))
  If Right(JoinSubs, 1) = ";" Then JoinSubs = Left(JoinSubs, Len(JoinSubs) - 1)
End Function

You would now call my UDF this way...

=JoinSubs(A2:A6,B2:B6)

and if you plan to drag the formula across, the make the Substance range absolute and leave the Quality column relative...

=JoinSubs($A2:$6,B2:B6)
 
Upvote 0
sorry, one more thing, your code displays a great deal of semi-colons. Do you know how to remove them?

Sorry, I used bad examples during testing. This should work correctly for you now...

Code:
Function JoinSubs(SubstanceRange As Range, QualityRange As Range) As String
  Dim X As Long, LastRow As Long, Txt As String
  LastRow = Cells(Rows.Count, SubstanceRange.Column).End(xlUp).Row
  JoinSubs = Join(Application.Transpose(Evaluate("IF(" & QualityRange.Address & "=0,""""," & _
             SubstanceRange.Address & "&"", ""&" & QualityRange.Address & ")")), "; ")
  Do While InStr(JoinSubs, "; ; ")
    JoinSubs = Replace(JoinSubs, "; ; ", "; ")
  Loop
  JoinSubs = Trim(JoinSubs)
  If Right(JoinSubs, 1) = ";" Then JoinSubs = Left(JoinSubs, Len(JoinSubs) - 1)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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