0

i have 2 sheets , i want to find the same rows in 2 sheets , so i put the first row in array , and by a for next i define the first array ...then i define another array from second sheet , then i compare them .... why it doesn't work?

Sub compare()
    Dim n(4) As Variant
    Dim o(4) As Variant
    Dim i As Integer

    For i = 3 To 20    'satrha
        For j = 2 To 4     'por kardan
            n(j) = Sheets("guys").Cells(i, j)
        Next                'por kardan
        k = 3
        Do                  'hhhh
            For Z = 2 To 4     'por dovomi
                o(Z) = Sheets("p").Cells(k, Z)
            Next                  'por dovomi
            If n(j) = o(Z) Then
                Sheets("guys").Cells(i, 1) = Sheets("p").Cells(k, 2)
                flag = True
            Else
                flag = False
                k = k + 1
            End If
        Loop Until flag = False  'hhhhh
    Next             'satrha
End Sub
4

1 回答 1

0

从您现有的代码中猜测,我的以下代码将在找到匹配项时将工作表“p”列 B 中的值复制到工作表“guys”列 A。

Sub compare()
    Dim i As Integer
    Dim j As Integer
    Dim l As Integer
    l = Sheets("p").Range("B65535").End(xlUp).Row
    Debug.Print l
    For i = 3 To 20
        For j = 3 To l
            If Sheets("guys").Cells(i, 2).Value = Sheets("p").Cells(j, 2).Value And _
                Sheets("guys").Cells(i, 3).Value = Sheets("p").Cells(j, 3).Value And _
                Sheets("guys").Cells(i, 4).Value = Sheets("p").Cells(j, 4).Value Then
                Sheets("guys").Cells(i, 1).Value = Sheets("p").Cells(j, 2).Value
                Exit For
            End If
        Next
    Next
End Sub

注意到我Value在我的代码中明确说过。这将复制计算值(例如公式的结果)而不是“原始”内容。

于 2012-08-27T11:09:20.400 回答