返回列表 上一主題 發帖

[發問] 2儲存格中字串差異顯示

回復 1# free72921

請測試看看,謝謝

Sub test()
Dim xD, xD1, Ar(), Ar1(), T$, ky, L%, i&, j%, n%, n1%
Set xD = CreateObject("Scripting.Dictionary")
Set xD1 = CreateObject("Scripting.Dictionary")
xR = Range("b3")
For j = 0 To UBound(Split(xR, ","))
    T = Split(xR, ",")(j): xD(T) = 1
Next
xR1 = Range("c3")
For j = 0 To UBound(Split(xR1, ","))
    T = Split(xR1, ",")(j): xD1(T) = 1
Next
For Each ky In xD
If xD1(ky) <> 1 Then: ReDim Preserve Ar(n): Ar(n) = ky: n = n + 1
Next
For Each ky In xD1
If xD(ky) <> 1 Then: ReDim Preserve Ar1(n1): Ar1(n1) = ky: n1 = n1 + 1
Next
If n1 > 0 Then
    For j = 0 To UBound(Ar1)
        T = Ar1(j): L = Len(T): pos = InStr(Range("c3"), T)
        Range("c3").Characters(pos, L).Font.ColorIndex = 3
    Next
End If
If n > 0 Then
    For j = 0 To UBound(Ar)
        T = Ar(j): L = Len(T): pos = InStr(Range("b3"), T)
        Range("b3").Characters(pos, L).Font.ColorIndex = 3
    Next
End If
End Sub

TOP

本帖最後由 samwang 於 2021-8-30 07:30 編輯

請教各位大大,如果同時有2個以上的差異,後學目前只能將第1個差異反顏色(2#程式碼),
不知各位大大是否可以將其他的差異也一併反顏色? 感謝
如附圖,第2個R777要反顏色。
擷取11.PNG

TOP

本帖最後由 samwang 於 2021-8-30 11:45 編輯

回復 9# ML089

我是很粗心的人,只是剛好看到此問題,
另外,看到版主解答,也學習一下而想到解法,感謝。

Sub test()
Dim xD, xD1, Ar(), Ar1(), T$, ky, L%, i&, j%, n%, n1%, xP%, xP1
Set xD = CreateObject("Scripting.Dictionary")
Set xD1 = CreateObject("Scripting.Dictionary")
xR = Range("b3")
For j = 0 To UBound(Split(xR, ","))
    T = Split(xR, ",")(j): xD(T) = xD(T) + 1
Next
xR1 = Range("c3")
For j = 0 To UBound(Split(xR1, ","))
    T = Split(xR1, ",")(j): xD1(T) = xD1(T) + 1
Next
For Each ky In xD
If Not xD1.exists(ky) Then: ReDim Preserve Ar(n): Ar(n) = ky: n = n + 1
Next
For Each ky In xD1
If Not xD.exists(ky) Then: ReDim Preserve Ar1(n1): Ar1(n1) = ky: n1 = n1 + 1
Next
If n1 > 0 Then
    For j = 0 To UBound(Ar1)
        T = Ar1(j): L = Len(T)
        For j1 = 1 To xD1(T)
            If j1 = 1 Then
                pos = InStr(Range("c3"), T)
            Else
                pos = InStr(xP, Range("c3"), T)
            End If
            Range("c3").Characters(pos, L).Font.ColorIndex = 3
            xP = pos + 2
        Next
    Next
End If
If n > 0 Then
    For j = 0 To UBound(Ar)
        T = Ar(j): L = Len(T)
        For j1 = 1 To xD(T)
            If j1 = 1 Then
                pos = InStr(Range("b3"), T)
            Else
                pos = InStr(xP1, Range("b3"), T)
            End If
            Range("b3").Characters(pos, L).Font.ColorIndex = 3
            xP1 = pos + 2
        Next
    Next
End If
End Sub

TOP

本帖最後由 samwang 於 2021-8-30 14:31 編輯

回復 6# free72921

例如:
跳出詢問視窗【請選擇範圍A】
跳出詢問視窗【請選擇範圍B】
再以被選定的2個範圍開始進行比對
>> 如下,請測試看看,謝謝

Sub test2()
Dim R, xR, xR1, xD, xD1, Ar(), Ar1(), ky
Dim T$, L%, i&, j%, n%, n1%, xP%, xP1%, pos%
Set xD = CreateObject("Scripting.Dictionary")
Set xD1 = CreateObject("Scripting.Dictionary")
On Error Resume Next
Set xR = Application.InputBox("請選擇Range:", Type:=8)
Set xR1 = Application.InputBox("請選擇Range:", Type:=8)

For Each R In xR: For j = 0 To UBound(Split(R, ","))
    T = Split(R, ",")(j): xD(T) = xD(T) + 1
Next j: Next R
For Each R In xR1: For j = 0 To UBound(Split(R, ","))
        T = Split(R, ",")(j): xD1(T) = xD1(T) + 1
Next j: Next R
For Each ky In xD
If Not xD1.exists(ky) Then: ReDim Preserve Ar(n): Ar(n) = ky: n = n + 1
Next
For Each ky In xD1
If Not xD.exists(ky) Then: ReDim Preserve Ar1(n1): Ar1(n1) = ky: n1 = n1 + 1
Next

If n1 > 0 Then
    For j = 0 To UBound(Ar1)
        For Each R In xR1
            T = Ar1(j): L = Len(T): xP = 1
            For j1 = 1 To xD1(T)
                pos = InStr(xP, R, T, 0)
                If pos > 0 Then
                    R.Characters(pos, L).Font.ColorIndex = 3
                    xP = pos + 2
                End If
            Next j1
        Next R
    Next j
End If
If n > 0 Then
    For j = 0 To UBound(Ar)
        For Each R In xR
            T = Ar(j): L = Len(T): xP1 = 1
            For j1 = 1 To xD(T)
                pos = InStr(xP1, R, T, 0)
                If pos > 0 Then
                    R.Characters(pos, L).Font.ColorIndex = 3
                    xP1 = pos + 2
                End If
            Next j1
        Next R
    Next j
End If
End Sub

TOP

        靜思自在 : 人生不一定球球是好球,但是有歷練的強打者,隨時都可以揮棒。
返回列表 上一主題