G
Guest
Hello,
I need some help to learn how to remove duplicate values from a string array.
The string for example is "blue yellow yellow red" or "blue yellow red
yellow". What I am doing is... I split the string into a string array or 5
elements. I then create a 2nd array to add only unique values and then join
the 2nd array so that the string is "blue yellow red". My issue is I am not
sure how to add unique values only to the second array.
i am sure there is a simple way to do this but cannot figure it out.
here is the code that i have written...
Sub RemoveDuplicateWords()
Dim array1() As String
Dim array2() As String
Dim tmp1, tmp2, tmp3 As String
Dim i, a, b, c As Integer
Dim str As String
Dim repeat As Boolean
repeat = False
str = "blue yellow yellow red"
'str = "blue yellow red yellow" 'this works
array1 = Split(str, " ")
i = UBound(array1) - LBound(array1)
ReDim array2(0 To i)
c = 0
For a = 0 To i
tmp1 = ""
tmp = array1(a)
'Debug.Print tmp
For b = 0 To i
tmp1 = array2(b)
If tmp = tmp1 Then
repeat = True
Exit For
End If
Next b
If repeat = False Then
'Debug.Print tmp
array2(c) = tmp
c = c + 1
End If
Next a
tmp3 = Join(array2, " ")
Debug.Print tmp3
End Sub
I need some help to learn how to remove duplicate values from a string array.
The string for example is "blue yellow yellow red" or "blue yellow red
yellow". What I am doing is... I split the string into a string array or 5
elements. I then create a 2nd array to add only unique values and then join
the 2nd array so that the string is "blue yellow red". My issue is I am not
sure how to add unique values only to the second array.
i am sure there is a simple way to do this but cannot figure it out.
here is the code that i have written...
Sub RemoveDuplicateWords()
Dim array1() As String
Dim array2() As String
Dim tmp1, tmp2, tmp3 As String
Dim i, a, b, c As Integer
Dim str As String
Dim repeat As Boolean
repeat = False
str = "blue yellow yellow red"
'str = "blue yellow red yellow" 'this works
array1 = Split(str, " ")
i = UBound(array1) - LBound(array1)
ReDim array2(0 To i)
c = 0
For a = 0 To i
tmp1 = ""
tmp = array1(a)
'Debug.Print tmp
For b = 0 To i
tmp1 = array2(b)
If tmp = tmp1 Then
repeat = True
Exit For
End If
Next b
If repeat = False Then
'Debug.Print tmp
array2(c) = tmp
c = c + 1
End If
Next a
tmp3 = Join(array2, " ")
Debug.Print tmp3
End Sub