fork download
  1. Option Explicit
  2. Public Sub シート比較()
  3. Dim ws1 As Worksheet
  4. Dim ws2 As Worksheet
  5. Dim ws3 As Worksheet
  6. Dim maxrow1 As Long
  7. Dim maxrow2 As Long
  8. Dim row1 As Long
  9. Dim row2 As Long
  10. Dim row3 As Long
  11. Dim dicT1 As Object
  12. Dim dicT2 As Object
  13. Dim key As Variant
  14. Dim reason As String
  15. Set ws1 = Worksheets("得意先")
  16. Set ws2 = Worksheets("自社")
  17. Set ws3 = Worksheets("相違")
  18. maxrow1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row '最終行取得
  19. maxrow2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row '最終行取得
  20. Set dicT1 = CreateObject("Scripting.Dictionary")
  21. Set dicT2 = CreateObject("Scripting.Dictionary")
  22. If maxrow1 < 2 Then
  23. MsgBox (ws1.Name & "にデータなし")
  24. Exit Sub
  25. End If
  26. If maxrow2 < 2 Then
  27. MsgBox (ws2.Name & "にデータなし")
  28. Exit Sub
  29. End If
  30. '得意先読込、異常時終了する
  31. If Read_Sheet(ws1, dicT1, maxrow1) = False Then Exit Sub
  32. '自社先読込、異常時終了する
  33. If Read_Sheet(ws2, dicT2, maxrow2) = False Then Exit Sub
  34. '相違の3行目以降をクリア
  35. ws3.Rows("3:" & Rows.Count).ClearContents
  36. row3 = 3
  37. '得意先の注文番号を順に全て処理する
  38. For Each key In dicT1.keys
  39. row1 = dicT1(key)
  40. row2 = 0
  41. reason = ""
  42. If dicT2.Exists(key) = False Then
  43. '自社に注文番号がない場合
  44. reason = "B注番なし"
  45. Else
  46. row2 = dicT2(key)
  47. If ws1.Cells(row1, "E").Value = ws2.Cells(row2, "E").Value Then
  48. If ws1.Cells(row1, "D").Value <> ws2.Cells(row2, "D").Value Then
  49. '単価が等しく、個数が異なる場合
  50. reason = "個数違い"
  51. End If
  52. Else
  53. '単価が異なる場合
  54. reason = "単価違い"
  55. End If
  56. End If
  57. If reason <> "" Then
  58. '相違が発生した場合
  59. 'Aシートの1行を転記、相違理由を設定
  60. ws3.Cells(row3, "A").Resize(1, 6) = ws1.Cells(row1, "A").Resize(1, 6).Value
  61. ws3.Cells(row3, "G").Value = reason
  62. If row2 <> 0 Then
  63. '単価違い又は個数違いの場合、Bシートの1行を転記、相違理由を設定
  64. ws3.Cells(row3, "J").Resize(1, 6) = ws2.Cells(row2, "A").Resize(1, 6).Value
  65. ws3.Cells(row3, "P").Value = reason
  66. End If
  67. row3 = row3 + 1
  68. End If
  69. Next
  70. '自社の注文番号を順に全て処理する
  71. For Each key In dicT2.keys
  72. row2 = dicT2(key)
  73. If dicT1.Exists(key) = False Then
  74. '得意先に注文番号がない場合、Bシートの1行を転記、相違理由を設定
  75. ws3.Cells(row3, "J").Resize(1, 6) = ws2.Cells(row2, "A").Resize(1, 6).Value
  76. ws3.Cells(row3, "P").Value = "A注番なし"
  77. row3 = row3 + 1
  78. End If
  79. Next
  80. MsgBox ("完了 相違件数=" & row3 - 3)
  81. End Sub
  82.  
  83.  
  84. Private Function Read_Sheet(ByRef ws As Worksheet, ByRef dicT As Object, ByVal maxrow As Long) As Boolean
  85. Dim wrow As Long
  86. Dim key As Variant
  87. Read_Sheet = False
  88. '2行目~最終行まで処理
  89. For wrow = 2 To maxrow
  90. key = ws.Cells(wrow, "C").Value
  91. If key = "" Then
  92. Call err_msg(ws, wrow, "C", "注文番号が空白です")
  93. Exit Function
  94. End If
  95. If ws.Cells(wrow, "D").Value = "" Then
  96. Call err_msg(ws, wrow, "D", "個数が空白です")
  97. Exit Function
  98. End If
  99. If ws.Cells(wrow, "E").Value = "" Then
  100. Call err_msg(ws, wrow, "E", "単価が空白です")
  101. Exit Function
  102. End If
  103. If dicT.Exists(key) = True Then
  104. Call err_msg(ws, wrow, "C", "注文番号が重複しています")
  105. Exit Function
  106. End If
  107. '辞書へ注文番号と行番号を登録
  108. dicT(key) = wrow
  109. Next
  110. Read_Sheet = True
  111. End Function
  112. Private Sub err_msg(ByRef ws As Worksheet, ByVal row_no As Long, ByVal col_no As String, ByVal errmsg As String)
  113. MsgBox ("シート=" & ws.Name & " 行番号=" & row_no & " " & errmsg)
  114. ws.Activate
  115. ws.Cells(row_no, col_no).Select
  116. End Sub
  117.  
  118.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty