Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Compare three strings?

Sometimes it's the little things that drive you crazy. I think I left my
brain at home.

How do you compare three strings? I want to make sure that none of three
variables are the same value. In a formula, I can do it, but the VBA code
seems to limit comparisons of only 2 at a time? Unless I'm missing something
simple. Any help?

Thanks in advance?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default Compare three strings?

If var1 < var2 And var1 < var3 And var2 < var3 Then
...

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"jayklmno" wrote in message
...
Sometimes it's the little things that drive you crazy. I think I left my
brain at home.

How do you compare three strings? I want to make sure that none of three
variables are the same value. In a formula, I can do it, but the VBA code
seems to limit comparisons of only 2 at a time? Unless I'm missing
something
simple. Any help?

Thanks in advance?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Compare three strings?

One way:

Dim bUnique As Boolean
bUnique = (strA < strB) And (strA < strC) And (strB < strC)
if bUnique Then
MsgBox "Strings are unique"
Else
MsgBox "Strings are not unique"
End If

In article ,
jayklmno wrote:

Sometimes it's the little things that drive you crazy. I think I left my
brain at home.

How do you compare three strings? I want to make sure that none of three
variables are the same value. In a formula, I can do it, but the VBA code
seems to limit comparisons of only 2 at a time? Unless I'm missing something
simple. Any help?

Thanks in advance?

  #5   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 207
Default Compare three strings?

How about something like:

Sub test()

a = 2
b = 1
c = 1

If Not Application.And(a = b, b = c, a = c) Then

MsgBox "different"
Else
MsgBox "same"
End If

End Sub

Good luck.

Ken
Norfolk, Va



On Feb 5, 5:16 pm, jayklmno
wrote:
Sometimes it's the little things that drive you crazy. I think I left my
brain at home.

How do you compare three strings? I want to make sure that none of three
variables are the same value. In a formula, I can do it, but the VBA code
seems to limit comparisons of only 2 at a time? Unless I'm missing something
simple. Any help?

Thanks in advance?





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Compare three strings?

Hello,

I suggest
IF var1 = var2 OR var1 = var3 OR var2 = var3 THEN
...
END IF

Regards,
Bernd

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Compare three strings?

Yet another approach is to use StrComp to compare the strings. StrComp is
faster and more efficient than use the "=" operator when comparing strings.

Function ThreeStringsDifferent(S1 As String, S2 As String, S3 As String, _
Optional ByVal CompareMode As VbCompareMethod) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''
' ThreeStringsDifferent
' This function compares S1, S2, and S3, and returns TRUE if at least one
' string is different from the others. It returns FALSE if all three strings
' are the same. If CompareMode is omitted or is any value other the
' vbBinaryCompare (case-sensitive), vbTextCompare is assumed
(case-insensitive).
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''
If CompareMode < vbBinaryCompare Then
CompareMode = vbTextCompare
End If

ThreeStringsDifferent = CBool((Abs(StrComp(S1, S2, CompareMode)) + _
Abs(StrComp(S2, S3, CompareMode))) + _
Abs(StrComp(S1, S3, CompareMode)))

End Function

You can specify in the CompareMode parameter whether the strings should be
compared ignoring upper and lower case (CompareMode omitted or anything
except vbBinaryCompare) or whether upper and lower case are to be considered
different (CompareMode = vbBinaryCompare).

You could generalize the function above to accept any number of strings.
This will accept up to 28 string values to compare. If they are all the
same, the function returns False. If at least one string differs from the
others, the result is True. Text comparison is determined by CompareMode. If
CompareMode is vbBinaryCompare, upper and lower case are considered
different. If CompareMode is any value other the vbBinaryCompare, the
comparison is case-insensitive (upper case = lower case).

Function StringsDifferent(ByVal CompareMode As VbCompareMethod, _
ParamArray Strings() As Variant) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
' StringsDifferent
' This returns TRUE if there is one string within Strings that is
' different from any other string in Strings. Case-sensitivity
' is determined by the CompareMode parameter. If CompareMode is
' any value other than vbBinaryCompare (case-sensitive),
' vbTextCompare is assumed (case-insensitive). You may supply up to
' 28 string values to test with function.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
Dim Ndx As Long
If CompareMode < vbBinaryCompare Then
CompareMode = vbTextCompare
End If

For Ndx = LBound(Strings) To UBound(Strings) - 1
If CBool(Abs(StrComp(Strings(Ndx), Strings(Ndx + 1), CompareMode))) Then
StringsDifferent = True
Exit Function
End If
Next Ndx
End Function



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"jayklmno" wrote in message
...
Sometimes it's the little things that drive you crazy. I think I left my
brain at home.

How do you compare three strings? I want to make sure that none of three
variables are the same value. In a formula, I can do it, but the VBA code
seems to limit comparisons of only 2 at a time? Unless I'm missing
something
simple. Any help?

Thanks in advance?



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
Compare Strings dksaluki Excel Discussion (Misc queries) 4 October 18th 08 09:39 PM
Compare two strings andy62 Excel Worksheet Functions 8 September 6th 06 02:14 PM
Compare Strings Question Sabo, Eric Excel Programming 5 February 24th 06 03:16 AM
Compare the strings yangyh Excel Discussion (Misc queries) 3 September 8th 05 04:45 AM
VBA search and compare strings stuart Excel Programming 1 June 11th 04 01:00 AM


All times are GMT +1. The time now is 10:39 PM.

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

About Us

"It's about Microsoft Excel"