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