Proving matched pair

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,822
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have 4 strings. How might I use VBA to determine if two and identical and the other two are also identical.
Thanks. I sense this must be blindingy simple but I'm not finding a solution, without gerting really messy.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
So you want a True for two sets of matching pairs and False for anything else?

Also, what if all four strings are identical? Does that count as two matching pairs?
 
Upvote 0
The four will never be identical that's been proven beforehand.
And yes, True for two matched pairs, else false would be perfect.
 
Upvote 0
See if this is any use then. (It does return True for 4 identical strings)
VBA Code:
Sub TwoPairs()
  Dim FourStrings As Variant
  Dim s As String
  
  FourStrings = Split("Cat|big dog|big dog|Cat", "|")
  
  s = "|" & Join(FourStrings, "||") & "|"
  s = Replace(s, Left(s, InStr(1, s, "||")), "", , , 0)
  s = Replace(s, Left(s, InStr(1, s, "||")), "", , , 0)
  MsgBox s = vbNullString
End Sub
 
Upvote 0
Solution
That's pretty mind boggling Peter! I've thrown a heap of things at it and the true/false return never fails.
Many thanks.
 
Upvote 0
Actually, I just thought about it some more and it isn't robust. Try with
"Cat|big dog|big dog|big dog"


I don't have enough time now, but I think there should be an easy fix. I will get back to you later.
 
Upvote 0
I did have time to take a stab. Try this (almost untested) one.

Rich (BB code):
Sub TwoPairs()
  Dim FourStrings As Variant
  Dim s As String
  
  FourStrings = Split("Cat|big dog|big dog|big dog", "|")

  s = "|" & Join(FourStrings, "||") & "|"
  s = Replace(s, Left(s, InStr(1, s, "||")), "", , 2, 0)
  s = Replace(s, Left(s, InStr(1, s, "||")), "", , 2, 0)
  MsgBox s = vbNullString
End Sub
 
Upvote 0
I never had a test with 3 + 1 and should have! Maybe after deployed.
Now handles that - and needs idendical casing, which is preferred too.
Thanks for v2 :)
 
Upvote 0

Forum statistics

Threads
1,203,120
Messages
6,053,631
Members
444,674
Latest member
Fieldy1999

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top