I'm trying to have a way in Excel that allows me to say that
ABC-DEF is the same as DEF-ABC
Anyone know a trick on how to do that?
I'm trying to have a way in Excel that allows me to say that
ABC-DEF is the same as DEF-ABC
Anyone know a trick on how to do that?
See how to ask questions: https://stackoverflow.com/help/how-to-ask
Sometimes, but only sometimes, (featuring manchmal aber nur manchmal) you may get lucky posting a question like this getting answered:
Option Explicit
Public Sub TestMe()
Debug.Print CompareMe("ABC-DEF", "DEF-ABC")
Debug.Print CompareMe("ABC-DEF", "DEF-AAC")
End Sub
Public Function CompareMe(strCompareA As String, strCompareB As String, Optional strDelim = "-")
Dim arrA As Variant
Dim arrB As Variant
arrA = Split(strCompareA, strDelim)
arrB = Split(strCompareB, strDelim)
CompareMe = (arrA(0) = arrB(1) And arrA(1) = arrB(0))
End Function
Check the TestMe
sub, it prints the result in the console.
Furthermore, this is an interview algorithm question, asked by Google. This is a general working answer - reverse the whole string, then split by word and reverse each word. This is how to do it with VBA:
Option Explicit
Public Sub TestMe()
Debug.Print strReverseOrder("ABC-DEF-GHI")
Debug.Print strReverseOrder("ABC-DEF") = "DEF-ABC"
End Sub
Public Function strReverseOrder(strToReverse As String, Optional strDelim = "-")
Dim strWorking As String
Dim arrWorking() As String
Dim lngCounter As Long
Dim strAnswer As String
strWorking = StrReverse(strToReverse)
arrWorking = Split(strWorking, strDelim)
For lngCounter = LBound(arrWorking) To UBound(arrWorking)
strAnswer = IIf(lngCounter = LBound(arrWorking), vbNullString, strAnswer & strDelim) & StrReverse(arrWorking(lngCounter))
Next lngCounter
strReverseOrder = strAnswer
End Function
The following worked for me:
=IF(D2>H2,CONCATENATE(D2,"-",H2),CONCATENATE(H2,"-",D2))
Assuming this is A1
put this in B1
: =IF(A1="ABC-DEF","DEF-ABC",A1)
. Drag down to fill in.