匹配/尋找不同工作表中的行

匹配/尋找不同工作表中的行

我有兩張表,其中包含商家、金額、資料等欄位。我想配對/找出工作表 1 + 工作表 2 中包含相同金額的行。在其中一張工作表中,我的資料比另一張工作表中的資料多,因此,例如,工作表1 中的第3 行與工作表2 中的第3 行不符。必須搜尋金額。

答案1

這應該可以,但你的問題很不清楚:(

Sub FindTheThings()

Dim resultRow As Integer
resultRow = 2

Dim currentRow As Integer
currentRow = 2

Worksheets("Sheet3").Range("A1").Value = "Sheet1 row"
Worksheets("Sheet3").Range("B1").Value = "Sheet2 row"

Do While (Worksheets("Sheet1").Range("B" & currentRow).Value <> "")

Dim amount As String
amount = Worksheets("Sheet1").Range("B" & currentRow).Value

Dim currentRowSheet2 As String
currentRowSheet2 = 2

    Do While (Worksheets("Sheet2").Range("B" & currentRowSheet2).Value <> "")

        Dim sheet2amount As String
        sheet2amount = Worksheets("Sheet2").Range("B" & currentRowSheet2).Value

        If (sheet2amount = amount) Then

            Worksheets("Sheet3").Range("A" & resultRow).Value = currentRow
            Worksheets("Sheet3").Range("B" & resultRow).Value = currentRowSheet2

            resultRow = resultRow + 1
            Exit Do
        End If

    currentRowSheet2 = currentRowSheet2 + 1
    Loop
currentRow = currentRow + 1

Loop

End Sub

存在錯誤,例如金額多次出現會發生什麼情況、sheet1資料的值多於或少於sheet2會發生什麼情況、資訊如何呈現等

不管怎樣,這個 VBa 應該可以讓你繼續下去,因為它可以調整

表1

在此輸入影像描述

表2

在此輸入影像描述

表 3(巨集運行後建立)

在此輸入影像描述

答案2

=VLOOKUP(B1,Sheet1!$A$1:$B$28,2,0)找到第一個實例。

=COUNTIF(Sheet1!$A:$A,Sheet2!B2)告訴是否有超過一個實例。

相關內容