2015-05-05 133 views
1

好吧,所以我設法在許多編碼專家的幫助下以某種方式解決了這些代碼。我需要創建一個比較兩個工作表中的數據的宏。Excel VBA比較列數據複製行

在我的兩個工作表中,有一個名爲「eRequest ID」一欄,我不得不復制切勿的記錄排在兩個文件有一個「eRequest ID」。

我現在制定的代碼現在複製在文件中具有「eRequest ID」的recrod。所以從邏輯上講,我必須在下面的代碼中「否定」IF條件,但是我不知道該怎麼做,因爲我是一個編碼方面的初學者,包括VBA。

Sub compareAndCopy() 
    Dim lastRowE As Integer 
    Dim lastRowF As Integer 
    Dim lastRowM As Integer 
    Dim foundTrue As Boolean 

    Application.ScreenUpdating = False 

    lastRowE = Sheets("JULY15Release_Master Inventory").Cells(Sheets("JULY15Release_Master Inventory").Rows.Count, "A").End(xlUp).Row 
    lastRowF = Sheets("JULY15Release_Dev status").Cells(Sheets("JULY15Release_Dev status").Rows.Count, "A").End(xlUp).Row 
    lastRowM = Sheets("Mismatch").Cells(Sheets("Mismatch").Rows.Count, "A").End(xlUp).Row 

    For i = 1 To lastRowE 
    foundTrue = False 
    For j = 1 To lastRowF 

    If Sheets("JULY15Release_Master Inventory").Cells(i, 2).Value = Sheets ("JULY15Release_Dev status").Cells(j, 7).Value Then 
     foundTrue = False 
     Exit For 
    End If 

    Next j 

    If Not foundTrue Then 
    Sheets("JULY15Release_Master Inventory").Rows(i).Copy Destination:= _ 
    Sheets("Mismatch").Rows(lastRowM + 1) 
    lastRowM = lastRowM + 1 

    End If 


    Next i 

    Application.ScreenUpdating = False 

    End Sub 

原諒我可憐的格式化..我很新的stackoverflow以及。

還有一件事,我剛剛意識到,這段代碼只複製Sheets("JULY15Release_Master Inventory")中的行,即使該數據只有一個「eRequest ID」,它也不會從Sheets("JULY15Release_Dev status")複製行。

+0

您的'foundTrue'變量從未設置爲true,因此您的if子句在第二個for循環後總是執行。所以每一行都會返回一個不匹配的結果。我想「eRequest ID」位於第2列的表一和第7列的表2中? – EngJon

+0

-EngJon「eRequest ID」位於兩張紙上的第1列。有沒有辦法讓這個代碼更簡單? –

+0

還有一件事,我剛剛意識到這個代碼只從'Sheets(「JULY15Release_Master Inventory」)複製行'它不會複製'Sheets(「JULY15Release_Dev status」)中的行,即使只有一個「eRequest ID」數據。 –

回答

0

您的「eRequest ID」位於列A的兩張表中,因此我會比較此列。如果兩張紙上都有匹配,則存在循環。如果沒有匹配,則將該行復制到「不匹配」。循環兩張紙。

Sub compareAndCopy() 
    Dim lastRowMaster As Integer 
    Dim lastRowDev As Integer 
    Dim lastRowMis As Integer 
    Dim foundTrue As Boolean 

    Application.ScreenUpdating = False 

    lastRowMaster = Sheets("JULY15Release_Master Inventory").Cells(Sheets("JULY15Release_Master Inventory").Rows.Count, "A").End(xlUp).Row 
    lastRowDev = Sheets("JULY15Release_Dev status").Cells(Sheets("JULY15Release_Dev status").Rows.Count, "A").End(xlUp).Row 
    lastRowMis = Sheets("Mismatch").Cells(Sheets("Mismatch").Rows.Count, "A").End(xlUp).Row 

    'start loop Master 
    For i = 2 To lastRowMaster '1 = headers 
    foundTrue = False 
    For j = 2 To lastRowDev 

    If Sheets("JULY15Release_Master Inventory").Cells(i, 1).Value = Sheets("JULY15Release_Dev status").Cells(j, 1).Value Then 
     foundTrue = True 
     Exit For 
    End If 

    Next j 

    If foundTrue = False Then 
    Sheets("JULY15Release_Master Inventory").Rows(i).Copy Destination:= _ 
    Sheets("Mismatch").Rows(lastRowMis + 1) 
    lastRowMis = lastRowMis + 1 

    End If 

    Next i 
    'end loop master 

    Sheets("Mismatch").Cells(lastRowMis + 2, 1).Value = "results from Dev" 
    lastRowMis = lastRowMis + 2 

    'start loop Dev 
    For i = 2 To lastRowDev '1 = headers 
    foundTrue = False 
    For j = 2 To lastRowMaster 

    If Sheets("JULY15Release_Dev status").Cells(i, 1).Value = Sheets("JULY15Release_Master Inventory").Cells(i, 1).Value Then 
     foundTrue = True 
     Exit For 
    End If 

    Next j 

    If foundTrue = False Then 
    Sheets("JULY15Release_Dev status").Rows(i).Copy Destination:= _ 
    Sheets("Mismatch").Rows(lastRowMis + 1) 
    lastRowMis = lastRowMis + 1 

    End If 

    Next i 
    'end loop dev 


    Application.ScreenUpdating = False 

    End Sub 
+0

你的意思是相同的格式?我改變了我的IF語句給你,並且它顯示了相同的結果.. –

+0

還有一件事,我剛剛意識到這個代碼只從'Sheets(「JULY15Release_Master Inventory」)複製行''它不會複製'Sheets(「JULY15Release_Dev狀態「),即使該數據只有一個」eRequest ID「。 –

+0

btw ...我可以知道** Sheets(「JULY15Release_Master Inventory」)中的值** 2 **。單元格(i,2).Value'爲??同樣,在** Sheets(「JULY15Release_Dev status」)** 7 **。Cells(j,7).Value' –