fork download
  1. Option Explicit
  2. Const K1 As String = "アクティオ"
  3. Const K2 As String = "クボタ建機"
  4. Const K3 As String = "コマツ"
  5. Dim dicK As Object
  6. Dim sh2 As Worksheet
  7. Public Sub リース集計()
  8. Dim sh As Worksheet
  9. Dim maxrow1 As Long
  10. Dim maxrow2 As Long
  11. Dim row1 As Long
  12. Dim row1s As Long
  13. Dim row2 As Long
  14. Dim dicT As Object
  15. Dim key As Variant
  16. Dim kikan As Long '期間
  17. Dim drow As Long 'データ表の行
  18. Dim dcol As Long 'データ表の列
  19. Dim mkikaku As Long '月極規格
  20. Dim mtanka As Long '月割単価
  21. Dim dtanka As Long '日割単価
  22. Dim hoshou As Long '保証料
  23. Dim kihon As Long '基本料
  24. Dim sho As Long '商(期間を月極規格で割った商)
  25. Dim amari As Long '余(期間を月極規格で割った余)
  26. Dim kingaku As Long '計算した金額
  27.  
  28. Call データ表読込
  29. Set sh = Worksheets("リース")
  30. Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  31. sh.Activate
  32. maxrow1 = sh.Cells(Rows.Count, "B").End(xlUp).Row
  33. maxrow2 = sh.Cells(Rows.Count, "I").End(xlUp).Row
  34. If maxrow2 > 3 Then
  35. sh.Range("I4:M" & maxrow2).Value = ""
  36. End If
  37. row2 = 4
  38. For row1 = 4 To maxrow1
  39. '会社名チェック
  40. If dicK.exists(sh.Cells(row1, "B").Value) = False Then
  41. MsgBox (row1 & "行 会社名エラー:" & sh.Cells(row1, "B").Value)
  42. Exit Sub
  43. End If
  44. '開始日設定時
  45. If sh.Cells(row1, "D").Value <> "" Then
  46. key = sh.Cells(row1, "B").Value & "|" & sh.Cells(row1, "C").Value
  47. If dicT.exists(key) = True Then
  48. MsgBox ("開始日重複")
  49. sh.Cells(row1, "D").Select
  50. End
  51. End If
  52. dicT(key) = row1 '行番号記憶
  53. End If
  54. '返却日設定時
  55. If sh.Cells(row1, "E").Value <> "" Then
  56. key = sh.Cells(row1, "B").Value & "|" & sh.Cells(row1, "C").Value
  57. If dicT.exists(key) = False Then
  58. MsgBox ("開始日未設定")
  59. sh.Cells(row1, "E").Select
  60. End
  61. End If
  62. row1s = dicT(key)
  63. sh.Cells(row2, "I").Value = sh.Cells(row1s, "B").Value 'リース会社
  64. sh.Cells(row2, "J").Value = sh.Cells(row1s, "C").Value '品目
  65. sh.Cells(row2, "K").Value = sh.Cells(row1s, "D").Value '開始日
  66. sh.Cells(row2, "L").Value = sh.Cells(row1, "E").Value '返却日
  67. sh.Cells(row2, "M").Value = sh.Cells(row2, "L").Value - sh.Cells(row2, "K").Value + 1 '期間
  68. '金額の算出
  69. '会社毎の品目を取得
  70. If dicK.exists(key) = True Then
  71. kikan = sh.Cells(row2, "M").Value '期間
  72. drow = dicK(key) \ 10000 'データ表の行
  73. dcol = dicK(key) Mod 10000 'データ表の列
  74. mkikaku = sh2.Cells(drow, dcol + 1).Value '月極規格
  75. mtanka = sh2.Cells(drow, dcol + 2).Value '月割単価
  76. dtanka = sh2.Cells(drow, dcol + 3).Value '日割単価
  77. hoshou = sh2.Cells(drow, dcol + 4).Value '保証料
  78. kihon = sh2.Cells(drow, dcol + 5).Value '基本料
  79. sho = kikan \ 30
  80. amari = kikan Mod 30
  81. If amari > mkikaku Then
  82. sho = sho + 1
  83. amari = amari - mkikaku
  84. End If
  85. kingaku = mtanka * sho + dtanka * amari + hoshou * kikan + kihon
  86. sh.Cells(row2, "N").Value = kingaku
  87. Else
  88. sh.Cells(row2, "N").Value = "品目なし"
  89. End If
  90. row2 = row2 + 1
  91. dicT.Remove (key)
  92. End If
  93. Next
  94. For Each key In dicT
  95. row1 = dicT(key)
  96. MsgBox ("返却日なし " & "行=" & row1 & " リース会社・品目=" & key & " 開始日=" & sh.Cells(row1, "D").Value)
  97. Next
  98. MsgBox ("集計完了")
  99. End Sub
  100.  
  101.  
  102. Private Sub データ表読込()
  103. Dim drow As Long
  104. Dim hin1 As String
  105. Dim hin2 As String
  106. Dim hin3 As String
  107. Dim key As String
  108. Set sh2 = Worksheets("データ表")
  109. Set dicK = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  110. dicK(K1) = True
  111. dicK(K2) = True
  112. dicK(K3) = True
  113. For drow = 112 To 147
  114. hin1 = sh2.Cells(drow, "B").Value
  115. hin2 = sh2.Cells(drow, "H").Value
  116. hin3 = sh2.Cells(drow, "N").Value
  117. '品目の行と列を記憶
  118. If hin1 <> "" Then
  119. key = K1 & "|" & hin1
  120. dicK(key) = drow * 10000 + 2
  121. End If
  122. If hin2 <> "" Then
  123. key = K2 & "|" & hin2
  124. dicK(key) = drow * 10000 + 8
  125. End If
  126. If hin3 <> "" Then
  127. key = K3 & "|" & hin3
  128. dicK(key) = drow * 10000 + 14
  129. End If
  130. Next
  131. End Sub
  132.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty