excel 无法从使用4个for循环编写的VBA代码中获取预期输出

m1m5dgzv  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(194)

我要比较2个清单。清单1填入栏A和B,清单2填入栏D和E。
我希望列表1和列表2中的共同项目以黄色突出显示。
如果某个项目不在列表1中,但在列表2中,我希望将其复制、粘贴到表2中。
如果一个项目出现在列表1中,但没有出现在列表2中,我希望他们复制,粘贴到工作表3。
我写了下面的代码,有着前面提到的目标。但是它没有给出预期的输出。有人能帮助我吗?请...
输入enter image description here
输出:enter image description here
工作表2 enter image description here
工作表3 enter image description here

Sub CompareTwoColumns()
  Dim compare1 As Variant, Compare2 As Variant
      Dim r As Integer, q As Integer, dif1 As Integer, dif2 As Integer, m As Integer, n As Integer
            Dim lr1 As Integer, lr2 As Integer
 
       
      lr1 = Range("a1").End(xlDown).Offset(-1, 0).Row
      lr2 = Range("d1").End(xlDown).Offset(-1, 0).Row
             
      For r = 3 To lr1
      Set compare1 = Cells(r, 1)
      
                
          For q = 3 To lr2
          Set Compare2 = Cells(q, 4)
          
          If compare1 = Compare2 Then GoTo z:
        
         
          Next q
          Range(Cells(r, 1), Cells(r, 1).End(xlToRight)).Copy
          dif1 = dif1 + 1
          Sheets(2).Cells(dif1, 1).PasteSpecial Paste:=xlPasteValues
z:
           Cells(q, 4).Interior.Color = vbYellow
           
           
           Next r
   
     For m = 3 To lr2
     Set Compare2 = Cells(m, 4)
     For n = 3 To lr1
     Set compare1 = Cells(n, 1)

     If Compare2 = compare1 Then GoTo y:
     
Next n

Range(Cells(n, 4), Cells(n, 4).End(xlToRight)).Copy
dif3 = dif3 + 1
Sheets(3).Cells(dif3, 1).PasteSpecial Paste:=xlPasteValues
 
y:

Cells(n, 1).Interior.Color = vbYellow
       
      Next m
     
     

 End Sub
iswrvxsc

iswrvxsc1#

If I understand you correctly ....
Before running the macro, the condition of the worksheets is something like this :

In Sheet DATA, there are two tables. First table range is in column A to B until whatever row, the second table range is in column D to E until whatever row. In the example of the image above, the last row with data of both tables ended in row 7.
The data in each column of both tables are not unique. As in the image in List1, there is "aaa" value which appear two times and in List2 there is "bbb" value which appear two times.
I want common items in List 1 and List 2 to be highlighted in yellow.
if there is value in column A which found in column D (and vice versa), the cell of this value will have yellow background color. So in the sample image, the expected result in List1 cell A3:B3, A4:B4, A5:B5 ... will be yellow and in List2 cell D2:E2, D3:E3, D5:E5, D6:E6 will be yellow.
If an item is present in List 1, but not present in List 2, I want them copy, pasted in sheet 3.
cell A1:B1, A6:B6 and A7:B7 will be copied to Sheet "NotInList2"
If an item is not present in List 1, but present in List 2, I want them copy, pasted in sheet 2.
cell D4:E4 and D7:E7 will be copied to Sheet "NotInList1"
So, the result will be something like the image below :

If your case is similar with the example above ...

Sub test()
Dim rg1 As Range: Dim rg2 As Range
Dim rgData As Range: Dim rg2Check As Range
Dim sh As Worksheet: Dim i As Integer
Dim arr: Dim el: Dim rg as Range

With Sheets("DATA")
    Cells.Interior.Color = xlNone
    Set rg1 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    Set rg2 = .Range("D2", .Range("D" & Rows.Count).End(xlUp))
End With

For i = 1 To 2
    Select Case i
        Case 1: Set rgData = rg1: Set rg2Check = rg2: Set sh = Sheets("NotInList2")
        Case 2: Set rgData = rg2: Set rg2Check = rg1: Set sh = Sheets("NotInList1")
    End Select

    Set arr = CreateObject("scripting.dictionary")
    For Each cell In rgData: arr.Item(cell.Value) = 1: Next

    For Each el In arr
        With rgData
            .Replace el, True, xlWhole, , False, , False, False
            Set rg = .SpecialCells(xlConstants, xlLogical)
            Set rg = Union(rg, rg.Offset(0, 1))
            .Replace True, el, xlWhole, , False, , False, False
        End With
        If Not rg2Check.Find(el, lookat:=xlWhole) Is Nothing Then
            rg.Interior.Color = vbYellow
        Else
            rg.Copy Destination:=sh.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    Next el
Next i

End Sub

Basically, the code loop two times to check List1 against List2 in the first loop, then check List2 againts List1 in the second loop.
In each of the outer loop, it create a unique value to check as arr variable.
Then it loop to each element in arr to get the cell address of the looped element, then it check if the looped element in arr is found in another List, then it put yellow color. If it doesn't, then it copy to the designated sheet.
Please note, the code compares what is in column A against what is in column D (and vice versa). It doesn't compare what is in column B against what is in column E (and vice versa).

Sub CompareTwoColumns()
Application.ScreenUpdating = False

Set sh1 = Sheets("NotInList1")
Set sh2 = Sheets("NotInList2")

    lr1 = Range("A" & Rows.Count).End(xlUp).Row
    lr2 = Range("D" & Rows.Count).End(xlUp).Row

    cek = False
    For r = 3 To lr1
      Set compare1 = Cells(r, 1)
          For q = 3 To lr2
            Set Compare2 = Cells(q, 4)
            If compare1 = Compare2 Then Compare2.Interior.Color = vbYellow: cek = True
          Next q
        If cek = False Then
            Range(compare1, compare1.End(xlToRight)).Copy
            sh2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        End If
    cek = False
    Next r
    
    cek = False
    For r = 3 To lr2
      Set compare1 = Cells(r, 4)
          For q = 3 To lr1
            Set Compare2 = Cells(q, 1)
            If compare1 = Compare2 Then Compare2.Interior.Color = vbYellow: cek = True
          Next q
        If cek = False Then
            Range(compare1, compare1.End(xlToRight)).Copy
            sh1.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        End If
    cek = False
    Next r

Application.ScreenUpdating = True
End Sub

相关问题