ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Compare data strings? (https://www.excelbanter.com/excel-programming/348036-compare-data-strings.html)

Ed

Compare data strings?
 
I have a series of data strings I need to compare. I am looking for the one
string that has a character found in no other string. What is the best way
to approach this?

Ed



Leith Ross[_377_]

Compare data strings?
 

Hello Ed,

This routine scans a string character by character and stores the
number of times the character is found in an array. The array has 256
elements, one for each ASCII value. The result is a final string that
will contain the unique characters found. A unique character is one
that appears only once.


Code:
--------------------

Sub FindUniqueCharacters()

Dim Chars(255) As Integer
Dim N As Integer
Dim TestStr As String

TestStr = "ABCDEFABCDeF"

For I = 1 To Len(TestStr)
N = Asc(Mid(TestStr, I, 1))
Chars(N) = Chars(N) + 1
Next I

For I = 0 To 255
If Chars(I) = 1 Then
S = S & Chr$(I)
End If
Next I

End Sub

--------------------


Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=493209


Dave Peterson

Compare data strings?
 
This seems to work ok:

Option Explicit
Sub testme()

Dim myWords As Variant
Dim iCtr As Long
Dim wCtr As Long
Dim lCtr As Long
Dim myStr As String

myWords = Array("one", "two", "three", "four")

For wCtr = LBound(myWords) To UBound(myWords)
myStr = ""
For iCtr = LBound(myWords) To UBound(myWords)
If wCtr = iCtr Then
'do nothing
Else
myStr = myStr & myWords(iCtr)
End If
Next iCtr

For lCtr = 1 To Len(myWords(wCtr))
If InStr(1, myStr, Mid(myWords(wCtr), lCtr, 1), _
vbTextCompare) = 0 Then
MsgBox Mid(myWords(wCtr), lCtr, 1) & " in " & myWords(wCtr) _
& vbLf & " is not used in any other word!"
End If
Next lCtr

Next wCtr

End Sub


Ed wrote:

I have a series of data strings I need to compare. I am looking for the one
string that has a character found in no other string. What is the best way
to approach this?

Ed


--

Dave Peterson

Ed

Compare data strings?
 
Very nice, Dave!! Thank you!
Ed

"Dave Peterson" wrote in message
...
This seems to work ok:

Option Explicit
Sub testme()

Dim myWords As Variant
Dim iCtr As Long
Dim wCtr As Long
Dim lCtr As Long
Dim myStr As String

myWords = Array("one", "two", "three", "four")

For wCtr = LBound(myWords) To UBound(myWords)
myStr = ""
For iCtr = LBound(myWords) To UBound(myWords)
If wCtr = iCtr Then
'do nothing
Else
myStr = myStr & myWords(iCtr)
End If
Next iCtr

For lCtr = 1 To Len(myWords(wCtr))
If InStr(1, myStr, Mid(myWords(wCtr), lCtr, 1), _
vbTextCompare) = 0 Then
MsgBox Mid(myWords(wCtr), lCtr, 1) & " in " &

myWords(wCtr) _
& vbLf & " is not used in any other word!"
End If
Next lCtr

Next wCtr

End Sub


Ed wrote:

I have a series of data strings I need to compare. I am looking for the

one
string that has a character found in no other string. What is the best

way
to approach this?

Ed


--

Dave Peterson





All times are GMT +1. The time now is 05:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com