fork download
  1. Option Explicit
  2.  
  3. Public Sub 必須項目チェック()
  4. Const Basecol As Long = 6 'E列
  5. Dim maxrow As Long '最大行
  6. Dim maxcol As Long '最大列
  7. Dim wrow As Long '行番号
  8. Dim wcol As Long '列番号
  9. Dim ret As Long
  10. Dim ws As Worksheet '本シート
  11. Dim mval() As String '必須の値
  12. Dim flag() As Boolean '必須フラグ(True:必須、False:必須でない)
  13. Dim wstr As String '文字(数値)
  14. Dim cnt As Long '件数カウンター
  15. Set ws = ActiveSheet
  16. maxrow = ws.Cells(Rows.Count, Basecol).End(xlUp).Row 'F列の最大行取得
  17. maxcol = ws.Cells(2, Columns.Count).End(xlToLeft).Column '2行目の最終列を求める
  18. If maxrow < 4 Then Exit Sub
  19. If maxcol < 2 Then Exit Sub
  20. ReDim mval(1 To maxcol)
  21. ReDim flag(1 To maxrow, 1 To maxcol)
  22. 'フラグクリア
  23. For wrow = 1 To maxrow
  24. For wcol = 1 To maxcol
  25. flag(wrow, wcol) = False
  26. Next
  27. Next
  28. '背景色クリア
  29. ws.Range(ws.Cells(4, 2), ws.Cells(maxrow, maxcol)).Interior.Pattern = xlNone
  30. '必須の値作成
  31. For wcol = 1 To maxcol
  32. '「○○の必須」から「○○」を取り出す
  33. mval(wcol) = Replace(ws.Cells(2, wcol).Value, "の必須", "")
  34. Next
  35. 'F列の値が必須となるセルを記憶する
  36. '4行~最終行まで繰り返す
  37. For wrow = 4 To maxrow
  38. wstr = ws.Cells(wrow, Basecol).Value
  39. If wstr = "" Then
  40. MsgBox ("F列の" & wrow & "行が空白です")
  41. ws.Cells(wrow, Basecol).Select
  42. Exit Sub
  43. End If
  44. cnt = 0
  45. '2列~最終列まで繰り返す
  46. For wcol = 2 To maxcol
  47. If wcol <> Basecol Then
  48. '必須の値が一致するなら該当セルのフラグへTrueを設定
  49. If wstr = mval(wcol) Then
  50. flag(wrow, wcol) = True
  51. cnt = cnt + 1
  52. End If
  53. End If
  54. Next
  55. '1件も一致する列がないならエラー
  56. If cnt = 0 Then
  57. MsgBox ("F列の" & wrow & "行の値が不正です")
  58. ws.Cells(wrow, Basecol).Select
  59. Exit Sub
  60. End If
  61. Next
  62. '必須となるセルに値が記入されているかチェックする
  63. cnt = 0
  64. 'チェック範囲の全セルをチェックする
  65. For wrow = 4 To maxrow
  66. For wcol = 2 To maxcol
  67. '必須であるセルが空白なら、背景色を設定する
  68. If flag(wrow, wcol) = True Then
  69. If ws.Cells(wrow, wcol) = "" Then
  70. ws.Cells(wrow, wcol).Interior.Color = 49407 'オレンジ
  71. cnt = cnt + 1
  72. End If
  73. End If
  74. Next
  75. Next
  76. If cnt = 0 Then
  77. MsgBox ("チェック完了")
  78. Else
  79. MsgBox ("必須項目データなし=" & cnt & "件")
  80. End If
  81. End Sub
  82.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty