fork download
  1. Option Explicit
  2.  
  3. Public Sub 新比較()
  4. Dim ssh As Worksheet
  5. Dim msh As Worksheet
  6. Dim sbsh As Worksheet
  7. Dim wrow As Long
  8. Dim wcol As Long
  9. Dim wcols As String
  10. Dim trow As Long
  11. Dim tcol As Long
  12. Dim tcols As String
  13. Dim maxrow_s, maxrow_m, maxcol_m, maxrow_t, maxcol_t As Long
  14. Dim dicT1 As Object '項目管理(シート1)
  15. Dim dicT2 As Object '項目管理(シート2)
  16. Dim dicTw As Object '項目管理
  17. Dim dicN1 As Object '項目管理(シート1)
  18. Dim dicN2 As Object '項目管理(シート2)
  19. Dim dicR As Object '追加2020/3/14
  20. Dim ser_m As String 'ユニークキー列(シート1)
  21. Dim ser_t As String 'ユニークキー列(シート2)
  22. Dim Key As Variant
  23. Dim item As Variant
  24. Dim RC As Variant
  25. Dim SetFile As Variant
  26. Dim newSh As Worksheet
  27. Dim mwb As Workbook
  28. Dim twb As Workbook
  29. Dim tsh As Worksheet
  30. Dim maxrow_s2 As Long '追加 回答者
  31. Dim rng As Range '追加 回答者
  32. Set ssh = Worksheets("設定")
  33. Set dicT1 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  34. Set dicT2 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  35. Set dicTw = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  36. Set dicN1 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  37. Set dicN2 = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  38. Set dicR = CreateObject("Scripting.Dictionary") ' 連想配列の定義 追加2020/3/14
  39.  
  40.  
  41.  
  42.  
  43. '/////////////////////////////////コモンダイアログから転記先のシートを開く
  44.  
  45. RC = MsgBox("書き込むファイルを選択してください。", vbYesNo + vbQuestion, "確認")
  46.  
  47. If RC = vbNo Then Exit Sub
  48. SetFile = Application.GetOpenFilename("Microsoft Excelブック,*.xls?")
  49. If SetFile = "False" Then
  50. MsgBox "キャンセルしました"
  51. Exit Sub
  52. End If
  53.  
  54. Set mwb = Workbooks.Open(SetFile)
  55. Set msh = mwb.Worksheets("管理表")
  56.  
  57. Worksheets.Add After:=msh
  58. Set newSh = ActiveSheet
  59. newSh.Name = "差分"
  60.  
  61. Set sbsh = Worksheets("差分")
  62.  
  63. '/////////////////////////////////後で書き換えます◎
  64.  
  65. Sheets("管理表").Select
  66. Rows("1:4").Select
  67. Selection.Copy
  68. Sheets("差分").Select
  69. Rows("1:1").Select
  70. ActiveSheet.Paste
  71.  
  72. Application.DisplayAlerts = True
  73. Application.CutCopyMode = True
  74.  
  75.  
  76. '/////////////////////////////////最大行の取得し、設定シートに記載した列を取得
  77.  
  78. maxrow_s = ssh.Cells(Rows.Count, "G").End(xlUp).Row 'A列の最大行取得
  79. maxrow_s2 = ssh.Cells(Rows.Count, "L").End(xlUp).Row '追加回答者 L列の最大行取得
  80. Set rng = ssh.Range("L2:L" & maxrow_s2) '追加回答者
  81. '設定シート列取得
  82. For wrow = 3 To maxrow_s
  83. dicT1(ssh.Cells(wrow, "G").Value) = ssh.Cells(wrow, "H").Value
  84. dicT2(ssh.Cells(wrow, "G").Value) = ssh.Cells(wrow, "I").Value
  85. dicR(ssh.Cells(wrow, "H").Value) = ssh.Cells(wrow, "I").Value '追加2020/3/14
  86. Next
  87.  
  88. If dicT1.Exists("ユニークキー") = False Then
  89. MsgBox ("設定シートに「ユニークキー」の項目なし")
  90. Exit Sub
  91. End If
  92.  
  93. ser_m = dicT1("ユニークキー")
  94. ser_t = dicT2("ユニークキー")
  95.  
  96. '各項目を記憶(シート1の列指定部分)
  97. maxcol_m = msh.Cells(4, Columns.Count).End(xlToLeft).Column '1行目の最終列を求める
  98. For wcol = 1 To maxcol_m
  99. dicTw(msh.Cells(4, wcol).Value) = wcol
  100. Next
  101.  
  102.  
  103. '項目存在チェック
  104. For Each item In dicT1
  105. If dicTw.Exists(item) = False Then
  106. MsgBox (item & "は管理表にありません")
  107. Exit Sub
  108. End If
  109. Next
  110.  
  111.  
  112. '項目の列位置チェック
  113. For Each item In dicT1
  114. wcols = dicT1(item)
  115. If msh.Cells(4, wcols).Value <> item Then
  116. msh.Cells(4, dicTw(item)).Interior.Color = vbYellow
  117. MsgBox ("処理中止しました")
  118. Exit Sub
  119. End If
  120. Next
  121.  
  122.  
  123. maxrow_m = msh.Cells(Rows.Count, ser_m).End(xlUp).Row 'sheet1 ユニークキー列の最大行取得
  124. 'ユニークキーの行を記憶
  125. For wrow = 4 To maxrow_m
  126. dicN1(msh.Cells(wrow, ser_m).Value) = wrow
  127. Next
  128. RC = MsgBox("参照するファイルを選択してください。", vbYesNo + vbQuestion, "確認")
  129. If RC = vbNo Then Exit Sub
  130. SetFile = Application.GetOpenFilename("Microsoft Excelブック,*.xls?")
  131. If SetFile = "False" Then
  132. MsgBox "キャンセルしました"
  133. Exit Sub
  134. End If
  135. Set twb = Workbooks.Open(SetFile)
  136. Set tsh = twb.Worksheets("管理表")
  137.  
  138.  
  139. '各項目を記憶
  140. maxcol_t = tsh.Cells(3, Columns.Count).End(xlToLeft).Column '1行目の最終列を求める
  141. dicTw.RemoveAll
  142. For wcol = 1 To maxcol_t
  143. dicTw(tsh.Cells(3, wcol).Value) = wcol
  144. Next
  145.  
  146.  
  147. '項目存在チェック
  148. For Each item In dicT2
  149. If dicTw.Exists(item) = False Then
  150. MsgBox (item & "は管理表にありません")
  151. Exit Sub
  152. End If
  153. Next
  154.  
  155.  
  156. '項目の列位置チェック
  157. For Each item In dicT2
  158. wcols = dicT2(item)
  159. If tsh.Cells(3, wcols).Value <> item Then
  160. MsgBox (item & "の列が異なります")
  161. Exit Sub
  162. End If
  163. Next
  164.  
  165.  
  166. maxrow_t = tsh.Cells(Rows.Count, ser_t).End(xlUp).Row 'sheet1 ユニークキー列の最大行取得
  167. 'ユニークキーの行を記憶
  168. For wrow = 3 To maxrow_t
  169. dicN2(tsh.Cells(wrow, ser_t).Value) = wrow
  170. Next
  171.  
  172.  
  173.  
  174. 'DE列をクリア
  175. msh.Range("DE2:DE" & maxrow_m).Value = ""
  176. msh.Range("DE2:DE" & maxrow_m).Interior.Pattern = xlNone
  177.  
  178.  
  179.  
  180. '/////////////////////////////////比較⇒処理
  181.  
  182. For wrow = 4 To maxrow_m
  183. Key = msh.Cells(wrow, ser_m).Value
  184.  
  185. 'If msh.Cells(wrow, "B").Value Like "*A*" _
  186. 'Or msh.Cells(wrow, "B").Value Like "*B*" Then
  187. If IfInstrs(msh.Cells(wrow, "B").Value, rng) = True Then '追加 回答者
  188. If msh.Cells(wrow, "G").Value = "設計OK" Or msh.Cells(wrow, "G").Value = "検討中" Then
  189. If dicN2.Exists(Key) = False Then
  190.  
  191.  
  192. 'シート1にありシート2にないユニークキーの処理
  193. msh.Cells(wrow, "DE").Value = "レコード削除"
  194. msh.Cells(wrow, "DE").Interior.Color = vbYellow
  195. Else
  196. trow = dicN2(Key)
  197. For Each item In dicR '修正2020/3/14
  198. If item <> ser_m Then '修正2020/3/14
  199. wcols = item '修正2020/3/14
  200. tcols = dicR(item) '修正2020/3/14
  201.  
  202.  
  203. If msh.Cells(wrow, wcols).Value = "" _
  204. And tsh.Cells(trow, tcols).Value = "" Then
  205.  
  206. Else
  207. '転記処理'
  208. If msh.Cells(wrow, "B").Value Like "*A*" _
  209. Or msh.Cells(wrow, "B").Value Like "*B*" Then
  210. If msh.Cells(wrow, "G").Value = "設計OK" Or msh.Cells(wrow, "G").Value = "検討中" Then
  211. If msh.Cells(wrow, wcols).Value = "" Or msh.Cells(wrow, wcols).Value = "-" Then
  212.  
  213. msh.Cells(wrow, wcols).Value = tsh.Cells(trow, tcols).Value
  214. msh.Cells(wrow, "DE").Value = "転記あり"
  215. msh.Cells(wrow, wcols).Interior.Color = vbCyan
  216. msh.Cells(wrow, wcols).Interior.Color = vbCyan
  217.  
  218. Else
  219. '元データがあり差分がある場合、DE列に「変更あり」を記載し、黄色く色付け
  220. '差分ファイルに転記し色付け
  221. If msh.Cells(wrow, "B").Value Like "*A*" _
  222. Or msh.Cells(wrow, "B").Value Like "*B*" Then
  223. If msh.Cells(wrow, "G").Value = "設計OK" Or msh.Cells(wrow, "G").Value = "検討中" Then
  224. If msh.Cells(wrow, wcols).Value <> tsh.Cells(trow, tcols).Value Then
  225.  
  226. msh.Cells(wrow, "DF").Interior.Color = vbYellow
  227. msh.Cells(wrow, "DF") = "変更あり"
  228. msh.Cells(wrow, wcols).Interior.Color = vbYellow
  229. sbsh.Cells(wrow, wcols).Value = tsh.Cells(trow, tcols).Value
  230. sbsh.Cells(wrow, wcols).Interior.Color = vbYellow
  231.  
  232. End If
  233. End If
  234. End If
  235. End If
  236. End If
  237. End If
  238. End If
  239. End If
  240. Next
  241. 'Next
  242. dicN2.Remove (Key)
  243. End If
  244. End If
  245. End If
  246. Next
  247.  
  248.  
  249. '/////////////////////////////////ユニークキーが追加されるようなことがある場合、ここをアクティブにする
  250.  
  251. 'シート2にありシート1にないユニークキーを追加する
  252. 'wrow = maxrow_m + 1
  253. 'For Each Key In dicN2
  254. ' trow = dicN2(Key)
  255. ' For Each item In dicR '修正2020/3/14
  256. ' wcols = item '修正2020/3/14
  257. ' tcols = dicR(item) '修正2020/3/14
  258. ' msh.Cells(wrow, wcols).Value = tsh.Cells(trow, tcols).Value
  259. ' Next
  260. ' wrow = wrow + 1
  261. 'Next
  262.  
  263.  
  264. '/////////////////////////////////ファイルを閉じて完了
  265.  
  266. Application.DisplayAlerts = False
  267. twb.Close False
  268. Application.DisplayAlerts = True
  269. MsgBox "完了しました"
  270. End Sub
  271.  
  272. Public Function IfInstrs(ByVal word As String, ByVal rng As Range) As Boolean
  273. Dim wrg As Range
  274. For Each wrg In rng
  275. If InStr(1, word, wrg.Value) > 0 Then
  276. IfInstrs = True
  277. Exit Function
  278. End If
  279. Next
  280. IfInstrs = False
  281. End Function
  282.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty