Solved

Change a string into Distinct elements

Posted on 2007-04-04
2
140 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
Comment Utility
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
Comment Utility
*** 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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now