回答編集履歴

3

想定仕様に説明を追記

2023/03/26 04:32

投稿

jinoji
jinoji

スコア4585

test CHANGED
@@ -8,11 +8,16 @@
8
8
  |0001|100|0|
9
9
  |0001|0|10|
10
10
 
11
+ - 同じ社員番号で複数レコードが存在する
12
+ - 給料および手当には、「正しい値」「空白」「0」のいずれかが入っている
13
+
11
14
  **OUTPUT(ディクショナリ)**
12
15
  |社員番号|給料|手当|
13
16
  |:--:|:--:|:--:|
14
17
  |0001|100|10|
15
18
 
19
+ - 社員番号で重複したデータを取り除き一意となった結果を出力
20
+ - 一意にする際、給料および手当は「正しい値」を採用
16
21
  ---
17
22
  ```VBA
18
23
  Sub sample()

2

想定する仕様について追記

2023/03/26 04:22

投稿

jinoji
jinoji

スコア4585

test CHANGED
@@ -1,3 +1,19 @@
1
+ やりたいことが以下のような仕様だと想像し、その想定のもとでコードを記述しました。
2
+
3
+ **INPUT(ファイル)**
4
+ |社員番号|給料|手当|
5
+ |:--:|:--:|:--:|
6
+ |0001|100||
7
+ |0001| |10|
8
+ |0001|100|0|
9
+ |0001|0|10|
10
+
11
+ **OUTPUT(ディクショナリ)**
12
+ |社員番号|給料|手当|
13
+ |:--:|:--:|:--:|
14
+ |0001|100|10|
15
+
16
+ ---
1
17
  ```VBA
2
18
  Sub sample()
3
19
  Open ファイル名 For Input As #1

1

teate, kyu 空白対応 他

2023/03/26 04:09

投稿

jinoji
jinoji

スコア4585

test CHANGED
@@ -1,28 +1,46 @@
1
1
  ```VBA
2
- Dim dic As Object
2
+ Sub sample()
3
- Set dic = CreateObject("Scripting.Dictionary")
3
+ Open ファイル名 For Input As #1
4
4
 
5
+ Dim dic As Object
6
+ Set dic = CreateObject("Scripting.Dictionary")
7
+
5
- Dim buf As Variant
8
+ Dim buf As Variant
6
- Dim tmp As Variant
9
+ Dim tmp As Variant
7
- Dim employeeNum As String
10
+ Dim employeeNum As String
8
- Dim teate As Long
11
+ Dim teate As Long
9
- Dim kyu As Long
12
+ Dim kyu As Long
13
+
14
+ Line Input #1, buf
15
+ Line Input #1, buf
16
+ Line Input #1, buf
17
+ Line Input #1, buf
18
+
19
+ Do Until EOF(1)
20
+ Line Input #1, buf
21
+ tmp = Split(buf, ",")
22
+
23
+ employeeNum = tmp(11)
24
+ teate = Val(tmp(45))
25
+ kyu = Val(tmp(40))
26
+
27
+ If dic.Exists(employeeNum) Then
28
+ If teate = 0 Then teate = dic(employeeNum)(1)
29
+ If kyu = 0 Then kyu = dic(employeeNum)(2)
30
+ End If
31
+
32
+ dic(employeeNum) = Array(employeeNum, teate, kyu)
33
+
34
+ Loop
10
35
 
36
+ Close #1
37
+
38
+ Dim k, v
39
+ For Each k In dic.Keys
40
+ v = dic(k)
11
- Line Input #1, buf
41
+ Debug.Print k, Join(v, vbTab)
42
+ Next
43
+
12
- Line Input #1, buf
44
+ End Sub
13
- Line Input #1, buf
14
- Line Input #1, buf
15
45
 
16
- Do Until EOF(1)
17
- Line Input #1, buf
18
- tmp = Split(buf, ",")
19
- employeeNum = tmp(11)
20
- teate = tmp(45)
21
- kyu = tmp(40)
22
- If dic.Exists(employeeNum) Then
23
- If teate = 0 Then teate = dic(employeeNum)(1)
24
- If kyu = 0 Then kyu = dic(employeeNum)(2)
25
- End If
26
- dic(employeeNum) = Array(employeeNum, teate, kyu)
27
- Loop
28
46
  ```