fork download
  1. Option Explicit
  2. Dim wks As Worksheet '設定対象シート
  3. Dim jks As Worksheet '条件シート
  4. Dim ids As Worksheet 'IDシート
  5. Dim dicT As Object '連想配列 キー:支店+拠点+担当者 値:G列の担当者主担当部門コード
  6. Public Sub ポイント付与()
  7. Dim sname As String '対象シート名
  8. Set jks = Worksheets("★条件")
  9. Set ids = Worksheets("★ID")
  10. Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  11. '対象シート取得
  12. sname = jks.Range("J2").Value
  13. If get_sheet(sname, wks) = False Then
  14. MsgBox ("シート名不正<" & sname & ">")
  15. Exit Sub
  16. End If
  17. Call setup_tanid '★IDシートを読み込み連想配列を作成する
  18. Application.ScreenUpdating = False
  19. Application.Calculation = xlCalculationManual
  20. 'ポイント作成開始
  21. Call add_point("F2", "P3", "B", "F", "G", "I", "J") 'J列のポイント
  22. Call add_point("F2", "P3", "B", "L", "M", "O", "P") 'P列のポイント
  23. '以下ここに追加してください
  24.  
  25. Application.Calculation = xlCalculationAutomatic
  26. Application.ScreenUpdating = True
  27. MsgBox ("完了")
  28. End Sub
  29.  
  30. 'P1: 主担当部門のセル位置 (ポイント取得用)
  31. 'P2: 担当者主担当部門のセル位置
  32. 'P3: 部門(拠点+部署+担当者)の開始列
  33. 'P4: 計画列
  34. 'P5: 実績列
  35. 'P6:達成/未達成列
  36. 'P7: ポイント付与列
  37. Private Sub add_point(mainbm_cell As String, tanbm_cell As String, bumon_col As String, kei_col As String, jis_col As String, tas_col As String, point_col As String)
  38. Dim maxrow As Long
  39. Dim wrow As Long
  40. Dim mpoint As Variant '満額ポイント
  41. Dim mainbm_val As Variant '主担当部門
  42. Dim point As Long '開始point
  43. Dim ap As Double 'point
  44. Dim bumon_colNo As Long '部門(拠点)の列番号
  45. Dim tan_id As String '担当ID(課別)若しくは担当者主担当部門コード
  46. Dim idrow As Long '★IDの行番号
  47. Dim key As Variant
  48. bumon_colNo = Range(bumon_col & "1").Column
  49. 'ポイント取得
  50. mainbm_val = wks.Range(mainbm_cell)
  51. mpoint = Application.HLookup(mainbm_val, jks.Range("B19:D21"), 3, 0)
  52. If IsError(mpoint) Then
  53. MsgBox ("★条件シートのポイント取得エラー:主担当部門<" & mainbm_val & ">")
  54. MsgBox ("算出ポイント列=" & point_col)
  55. Application.Calculation = xlCalculationAutomatic
  56. End
  57. End If
  58. point = mpoint
  59. maxrow = wks.Cells(Rows.Count, "A").End(xlUp).Row 'A列最終行
  60. wks.Range(point_col & "5:" & point_col & maxrow).Value = 0 'ポイント行を0クリア
  61. For wrow = 5 To maxrow
  62. '部門(拠点)なしなら終了
  63. If wks.Cells(wrow, bumon_col) = "" Then Exit For
  64. '該当行の担当者主担当部門コードを取得する
  65. key = wks.Cells(wrow, bumon_colNo) & "|" & wks.Cells(wrow, bumon_colNo + 1) & "|" & wks.Cells(wrow, bumon_colNo + 2)
  66. If dicT.exists(key) = False Then
  67. MsgBox ("★IDシートに未登録<" & key & ">")
  68. MsgBox ("行番号=" & wrow & " 部門(拠点)列=" & bumon_col & " 算出ポイント列=" & point_col)
  69. ap = 0
  70. Else
  71. tan_id = dicT(key)
  72. '該当行のポイントを取得する
  73. ap = get_point(wrow, tanbm_cell, tan_id, kei_col, jis_col, tas_col, point)
  74. End If
  75. wks.Cells(wrow, point_col).Value = ap
  76. '付与ポイントがあるなら、ポイントから1引く
  77. If ap <> 0 Then
  78. point = point - 1
  79. End If
  80. Next
  81. End Sub
  82. 'シート名取得
  83. Private Function get_sheet(sname As String, ws As Worksheet)
  84. On Error GoTo ERR99
  85. Set ws = Worksheets(sname)
  86. get_sheet = True
  87. Exit Function
  88. ERR99:
  89. get_sheet = False
  90. End Function
  91. '担当IDセットアップ
  92. Private Sub setup_tanid()
  93. Dim maxrow As Long
  94. Dim wrow As Long
  95. Dim key As Variant
  96. maxrow = ids.Cells(Rows.Count, "A").End(xlUp).Row 'A列最終行
  97. For wrow = 3 To maxrow
  98. key = ids.Cells(wrow, "B").Value & "|" & ids.Cells(wrow, "C").Value & "|" & ids.Cells(wrow, "D").Value
  99. dicT(key) = ids.Cells(wrow, "G").Value
  100. Next
  101. End Sub
  102. '付与ポイント取得
  103. Private Function get_point(wrow As Long, tanbm_cell As String, tan_id As String, kei_col As String, jis_col As String, tas_col As String, point As Long)
  104. Dim kei_val As Variant '計画
  105. Dim jis_val As Variant '実績
  106. Dim tas_val As Variant '達成/未達成
  107. Dim rate As Double 'ポイントレート%
  108. get_point = 0
  109. rate = 0
  110. kei_val = wks.Cells(wrow, kei_col)
  111. jis_val = wks.Cells(wrow, jis_col)
  112. tas_val = wks.Cells(wrow, tas_col)
  113. If kei_val <= 0 And jis_val <= 0 Then Exit Function
  114. If wks.Range(tanbm_cell).Value = tan_id Then
  115. '担当者主担当部門と担当IDが一致
  116. If tas_val > 0 Then
  117. '達成/未達成>0
  118. If kei_val >= 0 And jis_val > 0 Then rate = 100
  119. End If
  120. If tas_val < 0 Then
  121. '達成/未達成<0
  122. If kei_val > 0 Then rate = 75
  123. End If
  124. Else
  125. '担当者主担当部門と担当IDが不一致
  126. If tas_val > 0 Then
  127. '達成/未達成>0
  128. If kei_val = 0 And jis_val > 0 Then rate = 12.5
  129. If kei_val > 0 And jis_val > 0 Then rate = 25
  130. End If
  131. End If
  132. get_point = point * rate / 100
  133. End Function
  134.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty