How to prevent duplicates when building a string?

Macro_Nerd99

Board Regular
Joined
Nov 13, 2021
Messages
61
Office Version
  1. 365
I have this code that works great to build a string. However, it sometimes creates duplicates. How do I prevent this?
VBA Code:
For Each cl In ws.Range("B4:B" & lr)
 If cl.Offset(, 19).Value > imax Then
            
                        iViolations = iViolations + 1
                        cl.EntireRow.Interior.ColorIndex = 6
                        Highlighted_Trades = True
                        Violations_str = Violations_str & cl.Value & ","
  End If
Next cl
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
How about
VBA Code:
For Each Cl In ws.Range("B4:B" & LR)
 If Cl.Offset(, 19).Value > imax Then
            
                        iViolations = iViolations + 1
                        Cl.EntireRow.Interior.ColorIndex = 6
                        Highlighted_Trades = True
                        If InStr(1, Violations_str, Cl.Value, vbTextCompare) = 0 Then Violations_str = Violations_str & Cl.Value & ","
  End If
Next Cl
 
Upvote 0
Solution
How about
VBA Code:
For Each Cl In ws.Range("B4:B" & LR)
 If Cl.Offset(, 19).Value > imax Then
           
                        iViolations = iViolations + 1
                        Cl.EntireRow.Interior.ColorIndex = 6
                        Highlighted_Trades = True
                        If InStr(1, Violations_str, Cl.Value, vbTextCompare) = 0 Then Violations_str = Violations_str & Cl.Value & ","
  End If
Next Cl
Worked great! Thanks
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,799
Messages
6,126,976
Members
449,351
Latest member
Sylvine

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