Advertisement
YasserKhalil2019

T4091_Compare Two Strings Get Difference By UDF

Oct 11th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. https://excel-egy.com/forum/t4091
  2. ---------------------------------
  3.  
  4. Sub Compare_Two_Strings_Get_Difference_By_UDF()
  5. Dim a, i As Long
  6.  
  7. With Sheet1
  8. a = .Range("C2:E" & .Cells(Rows.Count, 3).End(xlUp).Row).Value
  9. For i = LBound(a) To UBound(a)
  10. a(i, 3) = Difference(CStr(a(i, 1)), CStr(a(i, 2)), " - ")
  11. Next i
  12. .Range("C2").Resize(UBound(a, 1), UBound(a, 2)).Value = a
  13. End With
  14. End Sub
  15.  
  16. Function Difference(txt1 As String, txt2 As String, Optional delim As String = ", ") As String
  17. Dim x, y, f As Boolean, s As String, i As Long, j As Long
  18.  
  19. If InStr(txt1, delim) > 0 And InStr(txt2, delim) > 0 Then
  20. x = Split(txt1, delim)
  21. y = Split(txt2, delim)
  22.  
  23. For i = LBound(x) To UBound(x)
  24. f = False
  25. For j = LBound(y) To UBound(y)
  26. If x(i) = y(j) Then f = True
  27. Next j
  28. If Not f Then s = s & IIf(s = "", "", delim) & x(i)
  29. Next i
  30.  
  31. For j = LBound(y) To UBound(y)
  32. f = False
  33. For i = LBound(x) To UBound(x)
  34. If y(j) = x(i) Then f = True
  35. Next i
  36. If Not f Then s = s & IIf(s = "", "", delim) & y(j)
  37. Next j
  38. End If
  39.  
  40. Difference = s
  41. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement