Concatenate IF criterias are met VBA

Zenru

New Member
Joined
Oct 19, 2017
Messages
29
Hello everybody, I have the need to concatenate multiple values if certain criterias are met.

I have a table that goes like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Color
[/TD]
[TD]Mat
[/TD]
[TD]Value
[/TD]
[TD]Val2
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]X100
[/TD]
[TD]40
[/TD]
[TD]X
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]D200
[/TD]
[TD]41
[/TD]
[TD]M
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]X100
[/TD]
[TD]45
[/TD]
[TD]T
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]D200
[/TD]
[TD]30
[/TD]
[TD]S
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]HD2O
[/TD]
[TD]45
[/TD]
[TD]T
[/TD]
[/TR]
[TR]
[TD]RED
[/TD]
[TD]X100
[/TD]
[TD]41
[/TD]
[TD]M
[/TD]
[/TR]
[TR]
[TD]RED
[/TD]
[TD]X100
[/TD]
[TD]44
[/TD]
[TD]R
[/TD]
[/TR]
</tbody>[/TABLE]

And I have another table I need to fill with the concatenated values in the other table:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Color
[/TD]
[TD]Mat
[/TD]
[TD]Value
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]X100
[/TD]
[TD]40, 45
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]D200
[/TD]
[TD]41
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]D200
[/TD]
[TD]30
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]HD2O
[/TD]
[TD]45
[/TD]
[/TR]
[TR]
[TD]RED
[/TD]
[TD]X100
[/TD]
[TD]41, 44
[/TD]
[/TR]
</tbody>[/TABLE]

If you notice, the UNIQUE value is COLOR&MAT. The values must be unique, not repeated. There might be instances where the COLOR&MAT will have repeated values, but they will be rare. So I need to modify this VBA code I found two be able to do this.

I don't know who wrote this code, and I am not sure if I can post the link to where I found it, but cheers to whoever wrote this!

TL;DR
The problem with this function is that it just accepts one criteria and it repeats all values. I need at least two criterias and for it to give back unique values only.

Code:
<code class="vb keyword">Function</code> <code class="vb plain">ConcatenateIf(CriteriaRange </code><code class="vb keyword">As</code> <code class="vb plain">Range, Condition </code><code class="vb keyword">As</code> <code class="vb keyword">Variant</code><code class="vb plain">, ConcatenateRange </code><code class="vb keyword">As</code> <code class="vb plain">Range, </code><code class="vb keyword">Optional</code> <code class="vb plain">Separator </code><code class="vb keyword">As</code> <code class="vb keyword">String</code> <code class="vb plain">= </code><code class="vb string">","</code><code class="vb plain">) </code><code class="vb keyword">As</code> <code class="vb keyword">Variant</code>

<code class="vb comments">'Update 20150414</code>

<code class="vb keyword">Dim</code> <code class="vb plain">xResult </code><code class="vb keyword">As</code> <code class="vb keyword">String</code>

<code class="vb keyword">On</code> <code class="vb keyword">Error</code> <code class="vb keyword">Resume</code> <code class="vb keyword">Next</code>
<code class="vb keyword">If</code> <code class="vb plain">CriteriaRange.Count <> ConcatenateRange.Count </code><code class="vb keyword">Then</code>
<code class="vb spaces">    </code><code class="vb plain">ConcatenateIf = CVErr(xlErrRef)</code>
<code class="vb spaces">    </code><code class="vb keyword">Exit</code> <code class="vb keyword">Function</code>
<code class="vb keyword">End</code> <code class="vb keyword">If</code>
<code class="vb keyword">For</code> <code class="vb plain">i = 1 </code><code class="vb keyword">To</code> <code class="vb plain">CriteriaRange.Count</code>
<code class="vb spaces">    </code><code class="vb keyword">If</code> <code class="vb plain">CriteriaRange.Cells(i).Value = Condition </code><code class="vb keyword">Then</code>
<code class="vb spaces">        </code><code class="vb plain">xResult = xResult & Separator & ConcatenateRange.Cells(i).Value</code>
<code class="vb spaces">    </code><code class="vb keyword">End</code> <code class="vb keyword">If</code>
<code class="vb keyword">Next</code> <code class="vb plain">i</code>
<code class="vb keyword">If</code> <code class="vb plain">xResult <> </code><code class="vb string">""</code> <code class="vb keyword">Then</code>
<code class="vb spaces">    </code><code class="vb plain">xResult = VBA.Mid(xResult, VBA.Len(Separator) + 1)</code>
<code class="vb keyword">End</code> <code class="vb keyword">If</code>
<code class="vb plain">ConcatenateIf = xResult</code>
<code class="vb keyword">Exit</code> <code class="vb keyword">Function</code>

<code class="vb keyword">End</code> <code class="vb keyword">Function</code>

Plus
I am not sure how hard will it be to do this, but if there is a way to concatenate an additional value in another column (VAL2) like this:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Color
[/TD]
[TD]Mat
[/TD]
[TD]Value
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]X100
[/TD]
[TD]40 - X, 45 - T
[/TD]
[/TR]
[TR]
[TD]BLUE
[/TD]
[TD]D200
[/TD]
[TD]41 - M
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]D200
[/TD]
[TD]30 - S
[/TD]
[/TR]
[TR]
[TD]YELLOW
[/TD]
[TD]HD2O
[/TD]
[TD]45 - T
[/TD]
[/TR]
[TR]
[TD]RED
[/TD]
[TD]X100
[/TD]
[TD]41 - M, 44
[/TD]
[/TR]
</tbody>[/TABLE]

I will GREATLY satisfied with my original request, but if someone can help me with the plus in one go I will very much appreciate it.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this for results on sheet2 starting "A1"
Code:
[COLOR="Navy"]Sub[/COLOR] MG30Oct42
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
Txt = Dn.Value & "," & Dn.Offset(, 1).Value
        [COLOR="Navy"]If[/COLOR] Not .Exists(Txt) [COLOR="Navy"]Then[/COLOR]
            .Add Txt, Dn.Offset(, 2).Value & "-" & Dn.Offset(, 3).Value
        [COLOR="Navy"]Else[/COLOR]
            .Item(Txt) = .Item(Txt) & ", " & Dn.Offset(, 2).Value & "-" & Dn.Offset(, 3).Value
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
ReDim Ray(1 To .Count + 1, 1 To 3)
Ray(1, 1) = "Color"
Ray(1, 2) = "Mat"
Ray(1, 3) = "Value"
c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    c = c + 1
    Ray(c, 1) = Split(K, ",")(0)
    Ray(c, 2) = Split(K, ",")(1)
    Ray(c, 3) = .Item(K)
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 3)
    .Value = Ray
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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