クラスと Scripting.Dictionary を使うと良いのでは?
vb
1Option Explicit
2
3Class Detail
4 Public Key
5 Public Amount
6 Public DateTime
7 Public Location
8 Public Name
9End Class
10
11Function LineToDetail(strLine)
12 Dim d, v
13 Set d = new Detail
14 v = Split(strLine, ",")
15 Redim Preserve v(4)
16 d.Key = v(0)
17 d.Amount = CCur(v(1))
18 d.DateTime = v(2)
19 d.Location = v(3)
20 d.Name = v(4)
21 Set LineToDetail = d
22End Function
23
24Dim dic, v, d
25Set dic = CreateObject("Scripting.Dictionary")
26
27For Each v in Array("001,2500,20220526,横浜,食料品", _
28 "005,1200,20220701,大船,雑貨", _
29 "001,3000,20220526,横浜,食料品")
30 Set d = LineToDetail(v)
31 If dic.Exists(d.Key) Then
32 dic(d.Key).Amount = dic(d.Key).Amount + d.Amount
33 Else
34 dic.Add d.Key, d
35 End If
36Next
37
38For Each d in dic.Items
39 MsgBox d.Key & "," & d.Amount & "," & d.DateTime & "," & d.Location & "," & d.Name
40Next
追記
クラスにメソッドやプロパティを追加して、マージの機能など持たせるといいかも
vb
1Option Explicit
2
3Class Detail
4 Public Key
5 Public Amount
6 Public DateTime
7 Public Location
8 Public Name
9
10 Public Sub Save(strLine)
11 v = Split(strLine, ",")
12 Redim Preserve v(4)
13 Key = v(0)
14 Amount = CCur(v(1))
15 DateTime = v(2)
16 Location = v(3)
17 Name = v(4)
18 End Sub
19
20 Public Sub Merge(d)
21 Amount = Amount + d.Amount
22 End Sub
23
24 Public Default Property Get Item
25 Item = Key & "," & Amount & "," & DateTime & "," & Location & "," & Name
26 End Property
27End Class
28
29Dim dic, v, d
30Set dic = CreateObject("Scripting.Dictionary")
31
32For Each v in Array("001,2500,20220526,横浜,食料品", _
33 "005,1200,20220701,大船,雑貨", _
34 "001,3000,20220526,横浜,食料品")
35 Set d = new Detail
36 d.Save(v)
37 If dic.Exists(d.Key) Then
38 dic(d.Key).Merge(d)
39 Else
40 dic.Add d.Key, d
41 End If
42Next
43
44For Each d in dic.Items
45 MsgBox d
46Next
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/30 05:35
2022/08/30 06:09
2022/08/30 06:38
2022/08/30 07:51
2022/08/30 08:01 編集
2022/08/30 08:47