?
Solved

Change a string into Distinct elements

Posted on 2007-04-04
2
Medium Priority
?
153 Views
Last Modified: 2010-04-30
Hi,
  I get a vb6 string  (Consists of various things listed seperated by comma)
  Example sring is
     "Mouse,Printer,CD,Printer,Cable,Keyboard,CD,Mouse trap,Cat food"
     should turn to  
      "Mouse,Printer,CD,Cable,Keyboard,Mouse trap,Cat food"

Thanking you,
          Sam  
 
0
Comment
Question by:sgs1970
2 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 375 total points
ID: 18849839
Dim I As Long, Arr
Dim col As New Collection
Dim s As String

On Error Resume Next
s = "Mouse,Printer,CD,Printer,Cable,Keyboard,CD,Mouse trap,Cat food"
Arr = Split(s, ",")
For I = 1 To UBound(Arr) + 1
    col.Add Arr(I - 1), Arr(I - 1)
Next
s = ""
For I = 1 To col.Count
    s = s & col(I) & ","
Next
Debug.Print Mid(s, 1, Len(s) - 1)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18851216
*** NOT FOR POINTS ***

I just put EDDYKT's code into a function and made some minor changes:

Option Explicit

Private Sub Command1_Click()
    Dim s As String
    s = "Mouse,Printer,CD,Printer,Cable,Keyboard,CD,Mouse trap,Cat food"
    Debug.Print "Before: " & s
    s = RemoveDuplicates(s)
    Debug.Print " After: " & s
End Sub

Private Function RemoveDuplicates(ByVal str As String, Optional ByVal delimeter As String = ",") As String
    Dim val As Variant
    Dim col As New Collection
   
    On Error Resume Next
    For Each val In Split(str, delimeter)
        col.Add val, val
    Next
    For Each val In col
        RemoveDuplicates = RemoveDuplicates & val & delimeter
    Next
    RemoveDuplicates = Left(RemoveDuplicates, Len(RemoveDuplicates) - Len(delimeter))
End Function
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question