I have problem in comparing cells with different value and give what ever is missing in one cell into third cell.
Example:
I am trying to match two cells:
cell 1 (abcd ) & cell 2 (cab) both have " abc" in common and I want the macro to show
missing value "d" to be shown in cell 3.
Consider:
Public Function WhatsMissing(Big As String, Little As String) As String
Dim V As String
V = Big
For i = 1 To Len(Little)
ch = Mid(Little, i, 1)
V = Replace(V, ch, "")
Next i
WhatsMissing = V
End Function
So if A1 contains abcdefg and B1 contains def then =WhatsMissing(A1,B1) would display:
abcg
If your values will have spaces in them for sure, then you can split them with the Split function and put them into arrays (or dictionary objects) and compare the two dictionaries for differences.
Here's a simple example:
Option Explicit
Sub getDifferences()
Dim s1() As String
Dim s2() As String
s1 = Split(Range("A1").Value, " ") ' a b c d
s2 = Split(Range("B1").Value, " ") ' c a b
Dim d1 As Object
Dim d2 As Object
Set d1 = CreateObject("Scripting.Dictionary")
Set d2 = CreateObject("Scripting.Dictionary")
' collect the values from cell 1
Dim i As Long
For i = 0 To UBound(s1)
d1.Add s1(i), i
Next
' collect the values from cell 2
For i = 0 To UBound(s2)
d2.Add s2(i), i
Next
Dim missing As Object
Set missing = CreateObject("Scripting.Dictionary")
Dim sKey As Variant
' check missing items from first cell to second
For Each sKey In d1.keys()
If (d2.exists(sKey) = False) Then
missing.Add sKey, 1
End If
Next
' check missing items from second cell to first
For Each sKey In d2.keys()
If (d1.exists(sKey) = False) Then
missing.Add sKey, 1
End If
Next
' display the missing items between the two
For Each sKey In missing.keys()
Debug.Print sKey
Next
End Sub
If cell 1 had: abcd
And cell 2 had: cabe
This will print out: de
Hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.