powercell99
Board Regular
- Joined
- May 14, 2014
- Messages
- 75
Good morning,
I found a great UDF that concatenates a range of data based on another range meeting 1 criteria. It works perfect, but I want to add another condition to the criteria but I cant get the wording correct.
Here is the UDF for the 1 condition:
Here is my attempt to add the additional criteria but its not working. Any suggestions?
Any suggestions or tips would be greatly appreciated
I found a great UDF that concatenates a range of data based on another range meeting 1 criteria. It works perfect, but I want to add another condition to the criteria but I cant get the wording correct.
Here is the UDF for the 1 condition:
HTML:
Function ConcatenateIf(CriteriaRange As Range, Condition As Variant, _
ConcatenateRange As Range, Optional Separator As String = " / ") As Variant
Dim i As Long
Dim strResult As String
On Error GoTo ErrHandler
If CriteriaRange.Count <> ConcatenateRange.Count Then
ConcatenateIf = CVErr(xlErrRef)
Exit Function
End If
For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition Then
strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i
If strResult <> "" Then
strResult = Mid(strResult, Len(Separator) + 1)
End If
ConcatenateIf = strResult
Exit Function
ErrHandler:
ConcatenateIf = CVErr(xlErrValue)
End Function
Here is my attempt to add the additional criteria but its not working. Any suggestions?
Code:
Function ConcatenateIfs(CriteriaRange1 As Range, Condition1 As Variant, CriteriaRange2 As Range, Condition2 As Variant, _
ConcatenateRange As Range, Optional Separator As String = " / ") As Variant
Dim i As Long
Dim strResult As String
On Error GoTo ErrHandler
If CriteriaRange1.Count <> ConcatenateRange.Count and CriteriaRange2.Count <> ConcatenateRange.CountThen
ConcatenateIfs = CVErr(xlErrRef)
Exit Function
End If
For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition Then
strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i
If strResult <> "" Then
strResult = Mid(strResult, Len(Separator) + 1)
End If
ConcatenateIf = strResult
Exit Function
ErrHandler:
ConcatenateIf = CVErr(xlErrValue)
End Function
Any suggestions or tips would be greatly appreciated