PDA

View Full Version : [SOLVED:] sort data within the same cell



TrainTrainer
02-12-2020, 01:51 PM
Issue:

Agents here assign each job multiple pieces of data as part of their work. For audit purposes, I receive 2 strings of data - agent response and correct response. Due to reasons beyond my control, the two strings come in different orders.

For instance, agent response may be (a, b, c, d, e, f, g) and correct response may be (c, a, b, d, g, f, e). They actually match, but are in different orders.

Within our analysis, the two strings have to be compared to determine if they are correct. Within my spreadsheet/VBA, I have a basic 1:1 comparison (=if(a1=b1, "correct", "incorrect")). Due to the difference in order, jobs are showing incorrect when they really are not. This adds time to sort these out.

I'm looking for the most efficient way to address this, whether it be re-ordering the string of data or doing a more complex comparison.

Any and all suggestions are appreciated.

Paul_Hossler
02-12-2020, 06:04 PM
I'd use a user defined function



Option Explicit


Sub test()
Dim s1 As String, s2 As String

s1 = "(a, b, c, d, e, f, g)"
s2 = "(c, a, b, d, g, f, e)"


MsgBox DoTheyMatch(s1, s2)


s2 = "(c, a, b, d, g, f, Z)"


MsgBox DoTheyMatch(s1, s2)


End Sub


Function DoTheyMatch(s1 As String, s2 As String) As String
Dim v1 As Variant, v2 As Variant
Dim s1a As String, s2a As String, s2b As String
Dim i As Long

'replace spaces
s1a = Replace(s1, " ", vbNullString)
s2a = Replace(s2, " ", vbNullString)

'assume leading and trailing parens
s1a = Mid(s1a, 2, Len(s1a) - 2)
s2a = Mid(s2a, 2, Len(s2a) - 2)

v1 = Split(s1a, ",")
v2 = Split(s2a, ",")


s2b = Join(v2, "#") & "#"

'start check
DoTheyMatch = "Incorrect"

For i = LBound(v1) To UBound(v1)
If InStr(s2b, v1(i) & "#") = 0 Then Exit Function
Next i

DoTheyMatch = "Correct"


End Function