2017-03-05 69 views
0

我想比較不同工作簿中的一列。工作簿1中的列位於範圍C3中,而工作簿2中的列位於範圍L3中。它們之間的數據是相同的。我必須比較的列是關於預訂號碼。在比較列後,如果所有數據都匹配,所以結果是msgbox會彈出並顯示「全部匹配」,但如果不是,則msgbox將提供有關何種單元格不匹配的信息。如何比較不同工作簿中的一列使用vba

列的例子是

BOOKING NO 
631609 
631098 
631099 
629487 
629488 

的範圍通常我用這個類型

(Range("C3"), Range("C3").End(xlDown)) 

會有人幫我,因爲我很初學者在Excel

使用VBA
+0

如果所有項目都匹配,但順序不同? –

回答

0

你可以用這個

Option Explicit 

Sub CompareBooking() 
    Dim rng1 As Range, rng2 As Range 
    Dim iRow As Long 
    Dim diffs As String 

    With Workbooks("Booking1Wb").Worksheets("booking1") '<--| change "Booking1Wb" and "booking1" to, respectively, your actual workbook and its worksheet names where to find booking numbers from C3 downwards 
     Set rng1 = .Range("C3", .Cells(.Rows.count, "C").End(xlUp)) '<--| change "Booking2Wb" and "booking2" to, respectively, your actual workbook and its worksheet names where to find booking numbers from L3 downwards 
    End With 

    With Workbooks("Booking2Wb").Worksheets("booking2") 
     Set rng2 = .Range("L3", .Cells(.Rows.count, "L").End(xlUp)) 
    End With 

    For iRow = 1 To WorksheetFunction.Max(rng1.Rows.count, rng2.Rows.count) 
     If rng1(iRow) <> rng2(iRow) Then diffs = diffs & iRow & vbLf 
    Next 

    If diffs <> "" Then 
     MsgBox "Different booking numbers in rows:" & vbCrLf & vbCrLf & diffs 
    Else 
     MsgBox "All bookings match" 
    End If 
End Sub 
+0

謝謝你的作品! – anastasya24

+0

不客氣 – user3598756

+0

我可以再問你嗎?什麼是宏條件「當項目匹配,但順序不同」,但我希望它也算作匹配 – anastasya24

相關問題