
販売者、金額、データなどの列を含む 2 つのシートがあります。2 つのシート間で 100% 共通しているのは金額です。シート 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
金額が複数回出現した場合に何が起こるか、シート1のデータがシート2よりも多くの値または少ない値を持つ場合に何が起こるか、情報をどのように表示するかなど、欠陥があります。
いずれにせよ、このVBaは調整可能なので、
シート1
シート2
シート 3 (マクロ実行後に作成)
答え2
=VLOOKUP(B1,Sheet1!$A$1:$B$28,2,0)
最初のインスタンスを検索します。
=COUNTIF(Sheet1!$A:$A,Sheet2!B2)
インスタンスが複数あるかどうかを通知します。