?
Solved

Change a string into Distinct elements

Posted on 2007-04-04
2
Medium Priority
?
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

752 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