Solved

Change a string into Distinct elements

Posted on 2007-04-04
2
143 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 125 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 85

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 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