craigachan
Registered User.
- Local time
- Today, 12:16
- Joined
- Nov 9, 2007
- Messages
- 285
I'm pulling my hair out and need help.
I have a string of numbers separated by commas. I'm looking to remove any number that repeats.
Examples:
1,2,3,2 returns 1,2,3
3,4,5,3,6 returns 3,4,5,6
3,4,3,5,3,6 returns 3,4,5,6
This all works fine, but when I try 8,9,8,9 I get '8'. but I want 8,9
It seems to work with 3 numbers but not with 2.
I can't seem to figure out how to fix this. would appreciate someone looking at my code and giving solution, fix, or input.
Sort code
Thanks
I have a string of numbers separated by commas. I'm looking to remove any number that repeats.
Examples:
1,2,3,2 returns 1,2,3
3,4,5,3,6 returns 3,4,5,6
3,4,3,5,3,6 returns 3,4,5,6
This all works fine, but when I try 8,9,8,9 I get '8'. but I want 8,9
It seems to work with 3 numbers but not with 2.
I can't seem to figure out how to fix this. would appreciate someone looking at my code and giving solution, fix, or input.
Code:
Public Function RemoveDblNums(strNumbers As String)
strNumbers = SortDelimitedStringOfNumbers(strNumbers, ",") 'Sort Numbers first
Dim splitarray() As String
Dim i As Integer
Dim x As Integer
Dim N As String 'Number being tested
Dim strFinal As String
strFinal = ""
splitarray = Split(strNumbers, ",")
For i = 0 To UBound(splitarray)
For x = (i + 1) To UBound(splitarray)
If splitarray(i) <> splitarray(i + 1) Then
N = splitarray(i)
Else
N = "dup"
End If
Next x
If N <> "dup" Then
If strFinal = "" Then
strFinal = splitarray(i)
Else
strFinal = strFinal & "," & splitarray(i)
End If
End If
Next i
RemoveDblNums = strFinal
End Function
Sort code
Code:
Public Function SortDelimitedStringOfNumbers(pInString As String, pDelimiter As String) As String
Dim i As Long, ii As Long
Dim temp
Dim strParts() As String
strParts() = Split(pInString, pDelimiter)
For i = 1 To UBound(strParts)
strParts(i) = Trim(strParts(i))
Next
For i = 0 To UBound(strParts)
For ii = i To UBound(strParts)
If Val(strParts(i)) > Val(strParts(ii)) Then
temp = strParts(ii)
strParts(ii) = strParts(i)
strParts(i) = temp
End If
Next
Next
SortDelimitedStringOfNumbers = Join(strParts, pDelimiter)
End Function
Thanks