回答編集履歴

3

変数が全角になっていた

2022/08/30 08:13

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -90,7 +90,7 @@
90
90
  End If
91
91
  Next
92
92
 
93
- For Each in dic.Items
93
+ For Each d in dic.Items
94
94
  MsgBox d
95
95
  Next
96
96
  ```

2

dic.Items を使う

2022/08/30 04:21

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -3,43 +3,42 @@
3
3
  Option Explicit
4
4
 
5
5
  Class Detail
6
- Public Key
6
+ Public Key
7
- Public Amount
7
+ Public Amount
8
- Public DateTime
8
+ Public DateTime
9
- Public Location
9
+ Public Location
10
- Public Name
10
+ Public Name
11
11
  End Class
12
12
 
13
13
  Function LineToDetail(strLine)
14
- Dim d, v
14
+ Dim d, v
15
- Set d = new Detail
15
+ Set d = new Detail
16
- v = Split(strLine, ",")
16
+ v = Split(strLine, ",")
17
17
  Redim Preserve v(4)
18
- d.Key = v(0)
18
+ d.Key = v(0)
19
- d.Amount = CCur(v(1))
19
+ d.Amount = CCur(v(1))
20
- d.DateTime = v(2)
20
+ d.DateTime = v(2)
21
- d.Location = v(3)
21
+ d.Location = v(3)
22
- d.Name = v(4)
22
+ d.Name = v(4)
23
- Set LineToDetail = d
23
+ Set LineToDetail = d
24
24
  End Function
25
25
 
26
- Dim dic, v, d, k
26
+ Dim dic, v, d
27
27
  Set dic = CreateObject("Scripting.Dictionary")
28
28
 
29
29
  For Each v in Array("001,2500,20220526,横浜,食料品", _
30
30
  "005,1200,20220701,大船,雑貨", _
31
31
  "001,3000,20220526,横浜,食料品")
32
- Set d = LineToDetail(v)
32
+ Set d = LineToDetail(v)
33
- If dic.Exists(d.Key) Then
33
+ If dic.Exists(d.Key) Then
34
- dic(d.Key).Amount = dic(d.Key).Amount + d.Amount
34
+ dic(d.Key).Amount = dic(d.Key).Amount + d.Amount
35
35
  Else
36
- dic.Add d.Key, d
36
+ dic.Add d.Key, d
37
- End If
37
+ End If
38
38
  Next
39
39
 
40
- For Each k in dic
40
+ For Each d in dic.Items
41
- Set d = dic(k)
42
- MsgBox d.Key & "," & d.Amount & "," & d.DateTime & "," & d.Location & "," & d.Name
41
+ MsgBox d.Key & "," & d.Amount & "," & d.DateTime & "," & d.Location & "," & d.Name
43
42
  Next
44
43
  ```
45
44
 
@@ -51,48 +50,48 @@
51
50
  Option Explicit
52
51
 
53
52
  Class Detail
54
- Public Key
53
+ Public Key
55
- Public Amount
54
+ Public Amount
56
- Public DateTime
55
+ Public DateTime
57
- Public Location
56
+ Public Location
58
- Public Name
57
+ Public Name
59
58
 
60
- Public Sub Save(strLine)
59
+ Public Sub Save(strLine)
61
- v = Split(strLine, ",")
60
+ v = Split(strLine, ",")
62
- Redim Preserve v(4)
61
+ Redim Preserve v(4)
63
- Key = v(0)
62
+ Key = v(0)
64
- Amount = CCur(v(1))
63
+ Amount = CCur(v(1))
65
- DateTime = v(2)
64
+ DateTime = v(2)
66
- Location = v(3)
65
+ Location = v(3)
67
- Name = v(4)
66
+ Name = v(4)
68
- End Sub
67
+ End Sub
69
68
 
70
- Public Sub Merge(d)
69
+ Public Sub Merge(d)
71
- Amount = Amount + d.Amount
70
+ Amount = Amount + d.Amount
72
- End Sub
71
+ End Sub
73
72
 
74
- Public Default Property Get Item
73
+ Public Default Property Get Item
75
- Item = Key & "," & Amount & "," & DateTime & "," & Location & "," & Name
74
+ Item = Key & "," & Amount & "," & DateTime & "," & Location & "," & Name
76
- End Property
75
+ End Property
77
76
  End Class
78
77
 
79
- Dim dic, v, d, k
78
+ Dim dic, v, d
80
79
  Set dic = CreateObject("Scripting.Dictionary")
81
80
 
82
81
  For Each v in Array("001,2500,20220526,横浜,食料品", _
83
82
  "005,1200,20220701,大船,雑貨", _
84
83
  "001,3000,20220526,横浜,食料品")
85
- Set d = new Detail
84
+ Set d = new Detail
86
- d.Save(v)
85
+ d.Save(v)
87
- If dic.Exists(d.Key) Then
86
+ If dic.Exists(d.Key) Then
88
- dic(d.Key).Merge(d)
87
+ dic(d.Key).Merge(d)
89
88
  Else
90
- dic.Add d.Key, d
89
+ dic.Add d.Key, d
91
- End If
90
+ End If
92
91
  Next
93
92
 
94
- For Each k in dic
93
+ For Each in dic.Items
95
- MsgBox dic(k)
94
+ MsgBox d
96
95
  Next
97
96
  ```
98
97
 

1

クラスにメソッドやプロパティを追加

2022/08/30 04:16

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -31,9 +31,9 @@
31
31
  "001,3000,20220526,横浜,食料品")
32
32
  Set d = LineToDetail(v)
33
33
  If dic.Exists(d.Key) Then
34
- dic(d.key).Amount = dic(d.key).Amount + d.Amount
34
+ dic(d.Key).Amount = dic(d.Key).Amount + d.Amount
35
35
  Else
36
- dic.Add d.key, d
36
+ dic.Add d.Key, d
37
37
  End If
38
38
  Next
39
39
 
@@ -42,3 +42,57 @@
42
42
  MsgBox d.Key & "," & d.Amount & "," & d.DateTime & "," & d.Location & "," & d.Name
43
43
  Next
44
44
  ```
45
+
46
+ # 追記
47
+
48
+ クラスにメソッドやプロパティを追加して、マージの機能など持たせるといいかも
49
+
50
+ ```vb
51
+ Option Explicit
52
+
53
+ Class Detail
54
+ Public Key
55
+ Public Amount
56
+ Public DateTime
57
+ Public Location
58
+ Public Name
59
+
60
+ Public Sub Save(strLine)
61
+ v = Split(strLine, ",")
62
+ Redim Preserve v(4)
63
+ Key = v(0)
64
+ Amount = CCur(v(1))
65
+ DateTime = v(2)
66
+ Location = v(3)
67
+ Name = v(4)
68
+ End Sub
69
+
70
+ Public Sub Merge(d)
71
+ Amount = Amount + d.Amount
72
+ End Sub
73
+
74
+ Public Default Property Get Item
75
+ Item = Key & "," & Amount & "," & DateTime & "," & Location & "," & Name
76
+ End Property
77
+ End Class
78
+
79
+ Dim dic, v, d, k
80
+ Set dic = CreateObject("Scripting.Dictionary")
81
+
82
+ For Each v in Array("001,2500,20220526,横浜,食料品", _
83
+ "005,1200,20220701,大船,雑貨", _
84
+ "001,3000,20220526,横浜,食料品")
85
+ Set d = new Detail
86
+ d.Save(v)
87
+ If dic.Exists(d.Key) Then
88
+ dic(d.Key).Merge(d)
89
+ Else
90
+ dic.Add d.Key, d
91
+ End If
92
+ Next
93
+
94
+ For Each k in dic
95
+ MsgBox dic(k)
96
+ Next
97
+ ```
98
+