新規 Microsoft Word 文書

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Sub 差分抽出()

Dim wsT As Worksheet


Dim wsR As Worksheet
Dim wsDB As Worksheet
Dim ws 結果 As Worksheet

Set wsT = Sheets("T")


Set wsR = Sheets("R")
Set wsDB = Sheets("DB")
Set ws 結果 = Sheets("結果")

'T のデータを上から順番に繰り返す

Lastrow = wsT.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To Lastrow

検索値 = wsT.Cells(i, 1)

'シート用の関数、COUNTIFS で R のシートの中から T の該当レコードの個数を取得

個数 = WorksheetFunction.CountIfs(wsR.Range("A:A"), 検索値)
個数 2 = WorksheetFunction.CountIfs(ws 結果.Range("A:A"), 検索値)

'関数の結果が 0 より大きいならば

If 個数 = 0 And 個数 2 = 0 Then
'転記先行を取得

Maxrow = ws 結果.Cells(Rows.Count, 1).End(xlUp).Row + 1

'差分を結果シートに転記

ws 結果.Cells(Maxrow, 1) = wsT.Cells(i, 1)
'DB シートの最終行を取得

maxrow2 = wsDB.Cells(Rows.Count, 1).End(xlUp).Row

For j = 2 To maxrow2

'もし、合致したら

If wsDB.Cells(j, 1) = 検索値 And ws 結果.Cells(Maxrow, 2) = "" Then

If wsDB.Cells(j, 3) = "池袋" Then

ws 結果.Cells(Maxrow, 3) = wsDB.Cells(j, 3)

ws 結果.Cells(Maxrow, 2) = wsDB.Cells(j, 2)

End If

End If

Next

For j = 2 To maxrow2

'もし、合致したら

If wsDB.Cells(j, 1) = 検索値 And ws 結果.Cells(Maxrow, 2) = "" Then

If wsDB.Cells(j, 3) = "新宿" Then

ws 結果.Cells(Maxrow, 3) = wsDB.Cells(j, 3)

ws 結果.Cells(Maxrow, 2) = wsDB.Cells(j, 2)
End If

End If

Next

For j = 2 To maxrow2

'もし、合致したら

If wsDB.Cells(j, 1) = 検索値 And ws 結果.Cells(Maxrow, 2) = "" Then

If wsDB.Cells(j, 3) = "銀座" Then

ws 結果.Cells(Maxrow, 3) = wsDB.Cells(j, 3)
‘列を追加するところ↓
ws 結果.Cells(Maxrow, 2) = wsDB.Cells(j, 2)

End If

End If

Next

For j = 2 To maxrow2

If wsDB.Cells(j, 1) = 検索値 Then

ws 結果.Cells(Maxrow, 2) = wsDB.Cells(j, 2)

End If
Next
End If
Next
End Sub

You might also like