• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Concatenate with criteria

Kim

Have a read through: http://chandoo.org/wp/2008/05/28/how-to-add-a-range-of-cells-in-excel-concat/

There is many different examples of Concatif() functions available there and in the comments

I modified a simple version to:
Code:
Function ConcatIf(Src As Range, ChkRng As Range, myVal As String, Optional Exc As Boolean = False, Optional Sep As String) As String
Dim c As Range
Dim retVal As String
Dim i As Integer

retVal = ""
i = 1

For Each c In ChkRng

  If Exc Then
  If c <> myVal Then
  If WorksheetFunction.IsNumber(Src(i)) Then
  retVal = retVal + Trim(Str(Src(i))) + Sep
  Else
  retVal = retVal + Src(i) + Sep
  End If
  End If
  Else
  If c <> myVal Then
  If WorksheetFunction.IsNumber(Src(i)) Then
  retVal = retVal + Trim(Str(Src(i))) + Sep
  Else
  retVal = retVal + Src(i) + Sep
  End If
  End If
  End If
  i = i + 1
Next
ConcatIf = Left(retVal, Len(retVal) - Len(Sep))
End Function

To use:
=ConcatIf(A1:C1,A2:C2,"",TRUE)

Then True tells excel to Exclude the ""

see attached file:
 

Attachments

Last edited:
Back
Top