Option Explicit
Const K1 As String = "アクティオ"
Const K2 As String = "クボタ建機"
Const K3 As String = "コマツ"
Dim dicK As Object
Dim sh2 As Worksheet
Public Sub リース集計()
Dim sh As Worksheet
Dim maxrow1 As Long
Dim maxrow2 As Long
Dim row1 As Long
Dim row1s As Long
Dim row2 As Long
Dim dicT As Object
Dim key As Variant
Dim kikan As Long '期間
Dim drow As Long 'データ表の行
Dim dcol As Long 'データ表の列
Dim mkikaku As Long '月極規格
Dim mtanka As Long '月割単価
Dim dtanka As Long '日割単価
Dim hoshou As Long '保証料
Dim kihon As Long '基本料
Dim sho As Long '商(期間を月極規格で割った商)
Dim amari As Long '余(期間を月極規格で割った余)
Dim kingaku As Long '計算した金額
Call データ表読込
Set sh = Worksheets("リース")
Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
sh.Activate
maxrow1 = sh.Cells(Rows.Count, "B").End(xlUp).Row
maxrow2 = sh.Cells(Rows.Count, "I").End(xlUp).Row
If maxrow2 > 3 Then
sh.Range("I4:M" & maxrow2).Value = ""
End If
row2 = 4
For row1 = 4 To maxrow1
'会社名チェック
If dicK.exists(sh.Cells(row1, "B").Value) = False Then
MsgBox (row1 & "行 会社名エラー:" & sh.Cells(row1, "B").Value)
Exit Sub
End If
'開始日設定時
If sh.Cells(row1, "D").Value <> "" Then
key = sh.Cells(row1, "B").Value & "|" & sh.Cells(row1, "C").Value
If dicT.exists(key) = True Then
MsgBox ("開始日重複")
sh.Cells(row1, "D").Select
End
End If
dicT(key) = row1 '行番号記憶
End If
'返却日設定時
If sh.Cells(row1, "E").Value <> "" Then
key = sh.Cells(row1, "B").Value & "|" & sh.Cells(row1, "C").Value
If dicT.exists(key) = False Then
MsgBox ("開始日未設定")
sh.Cells(row1, "E").Select
End
End If
row1s = dicT(key)
sh.Cells(row2, "I").Value = sh.Cells(row1s, "B").Value 'リース会社
sh.Cells(row2, "J").Value = sh.Cells(row1s, "C").Value '品目
sh.Cells(row2, "K").Value = sh.Cells(row1s, "D").Value '開始日
sh.Cells(row2, "L").Value = sh.Cells(row1, "E").Value '返却日
sh.Cells(row2, "M").Value = sh.Cells(row2, "L").Value - sh.Cells(row2, "K").Value + 1 '期間
'金額の算出
'会社毎の品目を取得
If dicK.exists(key) = True Then
kikan = sh.Cells(row2, "M").Value '期間
drow = dicK(key) \ 10000 'データ表の行
dcol = dicK(key) Mod 10000 'データ表の列
mkikaku = sh2.Cells(drow, dcol + 1).Value '月極規格
mtanka = sh2.Cells(drow, dcol + 2).Value '月割単価
dtanka = sh2.Cells(drow, dcol + 3).Value '日割単価
hoshou = sh2.Cells(drow, dcol + 4).Value '保証料
kihon = sh2.Cells(drow, dcol + 5).Value '基本料
sho = kikan \ 30
amari = kikan Mod 30
If amari > mkikaku Then
sho = sho + 1
amari = amari - mkikaku
End If
kingaku = mtanka * sho + dtanka * amari + hoshou * kikan + kihon
sh.Cells(row2, "N").Value = kingaku
Else
sh.Cells(row2, "N").Value = "品目なし"
End If
row2 = row2 + 1
dicT.Remove (key)
End If
Next
For Each key In dicT
row1 = dicT(key)
MsgBox ("返却日なし " & "行=" & row1 & " リース会社・品目=" & key & " 開始日=" & sh.Cells(row1, "D").Value)
Next
MsgBox ("集計完了")
End Sub
Private Sub データ表読込()
Dim drow As Long
Dim hin1 As String
Dim hin2 As String
Dim hin3 As String
Dim key As String
Set sh2 = Worksheets("データ表")
Set dicK = CreateObject("Scripting.Dictionary") ' 連想配列の定義
dicK(K1) = True
dicK(K2) = True
dicK(K3) = True
For drow = 112 To 147
hin1 = sh2.Cells(drow, "B").Value
hin2 = sh2.Cells(drow, "H").Value
hin3 = sh2.Cells(drow, "N").Value
'品目の行と列を記憶
If hin1 <> "" Then
key = K1 & "|" & hin1
dicK(key) = drow * 10000 + 2
End If
If hin2 <> "" Then
key = K2 & "|" & hin2
dicK(key) = drow * 10000 + 8
End If
If hin3 <> "" Then
key = K3 & "|" & hin3
dicK(key) = drow * 10000 + 14
End If
Next
End Sub