Solved

Change a string into Distinct elements

Posted on 2007-04-04
2
146 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 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

739 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