Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast string comparison

I'm looking to create a function that will compare two
strings A and B, and return True if string A is "less than" B.
Uppercase and/or lowercase should be ignored.

For example:

A B Value
--------- ------- -------
apple banana True
banana apple False
winner wins True
WiNneR winS True
right-handed left-handed False
apple apple False (equal)

My input will consist of only words that begin with only
alphanumberic characters, but may contain hyphens (-) or
apostrophes (') within the word.

Since I will be comparing a LARGE set of words, I'm
hoping for a comparison function that works as quickly
as possible.

Thanks!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast string comparison

Hi Robert,

Am Fri, 15 May 2015 05:35:23 -0700 schrieb Robert Crandal:

A B Value
--------- ------- -------
apple banana True
banana apple False
winner wins True
WiNneR winS True
right-handed left-handed False
apple apple False (equal)


try:
Function myComp(myRng As Range) As Boolean
Dim rngC As Range
Dim i As Long

For Each rngC In myRng
For i = 1 To Len(rngC)
If LCase(Mid(rngC, i, 1)) < _
LCase(Mid(rngC.Offset(, 1), i, 1)) Then
myComp = IIf(Asc(LCase(Mid(rngC, i, 1))) < _
Asc(LCase(Mid(rngC.Offset(, 1), i, 1))), True, False)
Exit For
End If
Next
Next
End Function

and call the function in the sheet with
=myComp(A1)

If nesessary you have to insert a Replace for hyphens and single quotes.


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast string comparison

Hi Robert,

Am Fri, 15 May 2015 14:52:56 +0200 schrieb Claus Busch:

For Each rngC In myRng


the first For ... Next statement is absolutly superfluous:

Function myComp(myRng As Range) As Boolean
Dim rngC As Range
Dim i As Long

For i = 1 To Len(rngC)
If LCase(Mid(rngC, i, 1)) < _
LCase(Mid(rngC.Offset(, 1), i, 1)) Then
myComp = IIf(Asc(LCase(Mid(rngC, i, 1))) < _
Asc(LCase(Mid(rngC.Offset(, 1), i, 1))), True, False)
Exit For
End If
Next

End Function


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast string comparison

Hi again,

Am Fri, 15 May 2015 14:59:03 +0200 schrieb Claus Busch:

For i = 1 To Len(rngC)
If LCase(Mid(rngC, i, 1)) < _
LCase(Mid(rngC.Offset(, 1), i, 1)) Then
myComp = IIf(Asc(LCase(Mid(rngC, i, 1))) < _
Asc(LCase(Mid(rngC.Offset(, 1), i, 1))), True, False)
Exit For
End If
Next


sorry but now rngC is wrong.

Try:

Function myComp(myRng As Range) As Boolean
Dim i As Long

For i = 1 To Len(myRng)
If LCase(Mid(myRng, i, 1)) < _
LCase(Mid(myRng.Offset(, 1), i, 1)) Then
myComp = IIf(Asc(LCase(Mid(myRng, i, 1))) < _
Asc(LCase(Mid(myRng.Offset(, 1), i, 1))), True, False)
Exit For
End If
Next

End Function


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast string comparison

"Claus Busch" wrote:

Am Fri, 15 May 2015 05:35:23 -0700 schrieb Robert Crandal:

A B Value
--------- ------- -------
apple banana True
banana apple False
winner wins True
WiNneR winS True
right-handed left-handed False
apple apple False (equal)


try:
Function myComp(myRng As Range) As Boolean
Dim rngC As Range
Dim i As Long

For Each rngC In myRng
For i = 1 To Len(rngC)
If LCase(Mid(rngC, i, 1)) < _
LCase(Mid(rngC.Offset(, 1), i, 1)) Then
myComp = IIf(Asc(LCase(Mid(rngC, i, 1))) < _
Asc(LCase(Mid(rngC.Offset(, 1), i, 1))), True, False)
Exit For
End If
Next
Next
End Function

and call the function in the sheet with
=myComp(A1)


Hi Claus. Unfortunately, I will not be comparing strings that
exist on a spreadsheet. I will be comparings strings that are
stored in String variables. So, for example:

Dim strA as String
Dim strB as String

strA = "apple"
strB = "banana"

If myComp(strA, strB) = True then
'
End if

I apologize for the bad explanation. My table above looked
like a spreadsheet, but really it was just a sample chart of
example data. So, I need a function that does not rely on
data stored in a spreadsheet. So sorry about that.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast string comparison

Hi Robert,

Am Fri, 15 May 2015 06:03:54 -0700 schrieb Robert Crandal:

Hi Claus. Unfortunately, I will not be comparing strings that
exist on a spreadsheet. I will be comparings strings that are
stored in String variables. So, for example:


then try:

Sub CompStrings()
Dim strA As String, strB As String
Dim i As Long
Dim myComp As Boolean

strA = "right-handed"
strB = "left-handed"

strA = Replace(Replace(strA, "-", ""), "'", "")
strB = Replace(Replace(strB, "-", ""), "'", "")

For i = 1 To Len(strA)
If LCase(Mid(strA, i, 1)) < LCase(Mid(strB, i, 1)) Then
myComp = IIf(Asc(LCase(Mid(strA, i, 1))) < _
Asc(LCase(Mid(strB, i, 1))), True, False)
MsgBox myComp
Exit For
End If
Next

End Sub

If your strings are in a 2D Array you could loop through the array and
insert the result into it and then write the array to a spreadsheet.


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast string comparison

"Claus Busch" wrote:

Sub CompStrings()

' snipped

End Sub



Thanks again Claus!


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast string comparison

"Claus Busch" wrote:

Sub CompStrings()
Dim strA As String, strB As String
Dim i As Long
Dim myComp As Boolean

strA = "right-handed"
strB = "left-handed"

strA = Replace(Replace(strA, "-", ""), "'", "")
strB = Replace(Replace(strB, "-", ""), "'", "")

For i = 1 To Len(strA)
If LCase(Mid(strA, i, 1)) < LCase(Mid(strB, i, 1)) Then
myComp = IIf(Asc(LCase(Mid(strA, i, 1))) < _
Asc(LCase(Mid(strB, i, 1))), True, False)
MsgBox myComp
Exit For
End If
Next

End Sub


Claus, do you think it will be faster to use the "<" or "" operators
to compare strings?

For example:

strA = "left-handed"
strB = "right-handed"

if (strA < strB) then
MsgBox "True"
else
MsgBox "False"
end if



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast string comparison

Hi Robert,

Am Sun, 17 May 2015 01:22:48 -0700 schrieb Robert Crandal:

Claus, do you think it will be faster to use the "<" or "" operators
to compare strings?


I don't have much experience working with text in Excel. But you are
right. You don't have to compare sign for sign, you can compare the
whole string.
Try:

Option Explicit
Option Compare Text

Sub StringCompare()
Dim StrA As String, strB As String

StrA = "left-handed"
strB = "right-handed"

If StrA < strB Then
MsgBox "True"
Else
MsgBox "False"
End If
End Sub

If you use
Option compare Binary
"AAA"< "aaa"
With Option Compare Text both are equal


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast way to truncate string Robert Crandal[_3_] Excel Programming 30 April 1st 15 11:43 PM
Fast string replacement Robert Crandal[_3_] Excel Programming 2 March 28th 15 02:59 AM
String Comparison [email protected] Excel Programming 5 July 7th 08 09:38 AM
how do I set up a fast comparison table? Biologist Excel Discussion (Misc queries) 1 June 21st 05 01:24 AM
Any fast method to parse a string into row & col information Nick Excel Programming 3 August 19th 04 06:47 PM


All times are GMT +1. The time now is 11:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"