• Source
    1. Option Explicit
    2.  
    3. Dim dicT As Object
    4. Dim sh1 As Worksheet
    5.  
    6. Public Sub D列の値を設定()
    7. Dim maxrow As Long
    8. Dim wrow As Long
    9. Set dicT = CreateObject("Scripting.Dictionary")
    10. Set sh1 = Worksheets("Sheet1")
    11. maxrow = sh1.Cells(Rows.Count, "A").End(xlUp).Row
    12. 'B列の数字を記憶
    13. For wrow = 1 To maxrow
    14. dicT(sh1.Cells(wrow, "B").Value) = wrow
    15. Next
    16. Call set_Dval("Sheet2")
    17. Call set_Dval("Sheet3")
    18. End Sub
    19. Private Sub set_Dval(ByVal wsname As String)
    20. Dim ws As Worksheet
    21. Dim maxrow As Long
    22. Dim maxcol As Long
    23. Dim wrow As Long
    24. Dim wcol As Long
    25. Dim val As Variant
    26. Dim val2 As String
    27. Dim row1 As Long
    28. Dim set_count As Long
    29. Dim skip_count As Long
    30. set_count = 0
    31. skip_count = 0
    32. Set ws = Worksheets(wsname)
    33. maxrow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
    34. maxcol = ws.UsedRange.Columns(ws.UsedRange.Columns.Count).Column
    35. For wrow = 1 To maxrow
    36. For wcol = 1 To maxcol
    37. val = ws.Cells(wrow, wcol)
    38. If val <> "" And dicT.exists(val) = True Then
    39. val2 = ws.Cells(wrow + 1, wcol).Value
    40. If val2 <> "" And ws.Cells(wrow + 1, wcol).MergeCells = True Then
    41. row1 = dicT(val)
    42. If sh1.Cells(row1, "C").Value = val2 Then
    43. ws.Cells(wrow, wcol + 1).Value = sh1.Cells(row1, "D").Value
    44. set_count = set_count + 1
    45. Else
    46. skip_count = skip_count + 1
    47. Debug.Print wsname, "行=" & wrow, "列=" & wcol
    48. End If
    49. End If
    50. End If
    51. Next
    52. Next
    53. MsgBox (wsname & "設定完了 設定件数=" & set_count & " スキップ件数=" & skip_count)
    54. End Sub
    55.  
    56.