teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

BA後に改修したコート→BA後のコメントに対してさらに改修したコード

2017/09/29 06:29

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -131,71 +131,106 @@
131
131
  ```
132
132
  とした後のElseIfの式はどうかきますか?
133
133
 
134
- BA後の改修したコード
134
+ BA後のコメントに対してさらに改修したコード
135
135
  ```vba
136
+ --セット条件に当てはまる引数をSetCellに渡す。
137
+ --pRow = 対象行数
138
+ --pClumn = 対象列数
136
- 'Eセルにデーセットするための分岐条件
139
+ --pDuaringPattern = 期間パーン
140
+ --pProgressSituation = 進捗状況
137
- Sub ECellSetter()
141
+ Sub SetCells(pClumn As Integer)
142
+
138
- For i = 6 To rowsCount
143
+ For pRow = 6 To rowsCount
144
+
145
+ --対象セルのデータを格納する。
139
- '作業着手予定日が空ならFor文から抜ける
146
+ pCellDate = Cells(pRow, pClumn).Value
147
+ --対象セルのデータの存在チェック
140
- If Cells(i, 2).Value = "" Then
148
+ If Cells(pRow, pClumn).Value = "" Then
141
149
  Exit For
142
150
  End If
143
- 'B6セルから末端までのデータを一時的に格納する。
144
- cellDateB = Cells(i, 2).Value
145
- 'D6セルから末端までのデータを一時的に格納する。
146
- cellDataD = Cells(i, 4).Value
147
151
 
152
+ --期間パターンを格納する変数。
153
+ Dim pDuringPattern As Integer
154
+ --期間パターンセット
148
- If cellDateB < cellDateFrom Then
155
+ If pCellDate < cellDateFrom Then
149
- Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
156
+ pDuringPattern = 0
150
- ElseIf cellDateB = cellDateFrom Then
157
+ ElseIf pCellDate = cellDateFrom Then
151
- Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
158
+ pDuringPattern = 1
152
- ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo Then
159
+ ElseIf cellDateFrom < pCellDate And pCellDate < cellDateTo Then
153
- Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
160
+ pDuringPattern = 2
154
- ElseIf cellDateB = cellDateTo Then
161
+ ElseIf pCellDate = cellDateTo Then
155
- Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
162
+ pDuringPattern = 3
156
- ElseIf cellDateTo < cellDateB Then
163
+ ElseIf cellDateTo < pCellDate Then
157
- Call SetCell(i, cellDataD, C_START_FIRST, C_START_FIRST, C_START_EMP, 5)
164
+ pDuringPattern = 4
158
165
  End If
159
166
 
167
+ --進捗状況を格納する変数
168
+ Dim pProgressSituation As Integer
169
+ --進捗状況セット
170
+ cellDataD = Cells(pRow, 4).Value
171
+ Select Case cellDataD
172
+ Case 100
173
+ pProgressSituation = 0
174
+ Case 0
175
+ pProgressSituation = 1
176
+ Case 1 To 99
177
+ pProgressSituation = 2
178
+ End Select
179
+
180
+ --セルに文字をセットする処理
181
+ Call SetCell(pRow, pClumn, pDuringPattern, pProgressSituation)
182
+
160
- Next i
183
+ Next pRow
184
+
161
185
  End Sub
162
186
 
187
+
188
+ --対象セルへの文字列セット処理。
189
+ --pString = セットする文字列
190
+ --pClumn = 2:着手状態 3:完了状況
191
+ --pProgressSituation = 0:進捗状況100% 1:進捗状況0% 2:進捗状況1~99%
192
+ --pDuringPattern = 期間のパターン 4:報告期間未満
193
+ Sub SetCell(pRow As Variant, pClumn As Integer, pDuringPattern As Integer, pProgressSituation As Integer)
194
+
163
- 'Fセルにデータセットするための分岐条件
195
+ --対象セルにセットする文字を格納する変数
164
- Sub FCellSetter()
165
- For i = 6 To rowsCount
196
+ Dim pString As String
197
+
166
- '作業着手予定日が空ならFor文から抜ける
198
+ If pClumn = 2 Then
167
- If Cells(i, 2).Value = "" Then
199
+ If pDuringPattern = 4 Then
200
+ Select Case pProgressSituation
168
- Exit For
201
+ Case 0, 2
202
+ pString = C_START_FIRST
203
+ Case 1
204
+ pString = C_START_EMP
205
+ End Select
206
+ Else
207
+ Select Case pProgressSituation
208
+ Case 0, 2
209
+ pString = C_START
210
+ Case 1
211
+ pString = C_START_LATE
212
+ End Select
169
213
  End If
170
- 'C6セルから末端までのデータを一時的に格納する。
171
- cellDateC = Cells(i, 3).Value
214
+ Else
172
- 'D6セルから末端までのデータを一時的に格納する。
173
- cellDataD = Cells(i, 4).Value
174
-
175
- If cellDateC < cellDateFrom Then
215
+ If pDuringPattern = 4 Then
176
- Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
177
- ElseIf cellDateC = cellDateFrom Then
178
- Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
179
- ElseIf cellDateFrom < cellDateC And cellDateC < cellDateTo Then
180
- Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
181
- ElseIf cellDateC = cellDateTo Then
182
- Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
183
- ElseIf cellDateTo < cellDateC Then
216
+ Select Case pProgressSituation
217
+ Case 0
184
- Call SetCell(i, cellDataD, C_END_FIRST, C_END_EMP, C_END_EMP, 6)
218
+ pString = C_END_FIRST
219
+ Case 1, 2
220
+ pString = C_END_EMP
221
+ End Select
222
+ Else
223
+ Select Case pProgressSituation
224
+ Case 0
225
+ pString = C_END
226
+ Case 1, 2
227
+ pString = C_END_LATE
228
+ End Select
185
229
  End If
186
-
187
- Next i
230
+ End If
231
+
232
+ --対象セルへの文字セット
233
+ Cells(pRow, pClumn + 3).Value = pString
188
234
  End Sub
189
-
190
- 'Eセル、Fセルに文字列をセットする処理
235
+
191
- Sub SetCell(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String, clumnNo As Integer)
192
- Select Case cellDataD
193
- Case 100
194
- Cells(count, clumnNo).Value = str1
195
- Case 1 To 99
196
- Cells(count, clumnNo).Value = str2
197
- Case 0
198
- Cells(count, clumnNo).Value = str3
199
- End Select
200
- End Sub
201
236
  ```

5

誤字修正

2017/09/29 06:29

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -197,4 +197,5 @@
197
197
  Case 0
198
198
  Cells(count, clumnNo).Value = str3
199
199
  End Select
200
- End Sub```
200
+ End Sub
201
+ ```

4

コードをさらに改修しました。

2017/09/28 09:21

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -146,33 +146,21 @@
146
146
  cellDataD = Cells(i, 4).Value
147
147
 
148
148
  If cellDateB < cellDateFrom Then
149
- Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
149
+ Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
150
150
  ElseIf cellDateB = cellDateFrom Then
151
- Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
151
+ Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
152
152
  ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo Then
153
- Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
153
+ Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
154
154
  ElseIf cellDateB = cellDateTo Then
155
- Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
155
+ Call SetCell(i, cellDataD, C_START, C_START, C_START_LATE, 5)
156
156
  ElseIf cellDateTo < cellDateB Then
157
- Call SetE(i, cellDataD, C_START_FIRST, C_START_FIRST, C_START_EMP)
157
+ Call SetCell(i, cellDataD, C_START_FIRST, C_START_FIRST, C_START_EMP, 5)
158
158
  End If
159
159
 
160
160
  Next i
161
161
  End Sub
162
162
 
163
- 'Eセルにデータセットする
163
+ 'Fセルにデータセットするための分岐条件
164
- Sub SetE(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String)
165
- Select Case cellDataD
166
- Case 100
167
- Cells(count, 5).Value = str1
168
- Case 1 To 99
169
- Cells(count, 5).Value = str2
170
- Case 0
171
- Cells(count, 5).Value = str3
172
- End Select
173
- End Sub
174
-
175
-
176
164
  Sub FCellSetter()
177
165
  For i = 6 To rowsCount
178
166
  '作業着手予定日が空ならFor文から抜ける
@@ -185,29 +173,28 @@
185
173
  cellDataD = Cells(i, 4).Value
186
174
 
187
175
  If cellDateC < cellDateFrom Then
188
- Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
176
+ Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
189
177
  ElseIf cellDateC = cellDateFrom Then
190
- Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
178
+ Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
191
179
  ElseIf cellDateFrom < cellDateC And cellDateC < cellDateTo Then
192
- Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
180
+ Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
193
181
  ElseIf cellDateC = cellDateTo Then
194
- Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
182
+ Call SetCell(i, cellDataD, C_END, C_END_LATE, C_END_LATE, 6)
195
183
  ElseIf cellDateTo < cellDateC Then
196
- Call SetF(i, cellDataD, C_END_FIRST, C_END_EMP, C_END_EMP)
184
+ Call SetCell(i, cellDataD, C_END_FIRST, C_END_EMP, C_END_EMP, 6)
197
185
  End If
198
186
 
199
187
  Next i
200
188
  End Sub
201
189
 
202
- 'Fセルにデータセットする
190
+ 'Eセル、Fセルに文字列をセットする処理
203
- Sub SetF(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String)
191
+ Sub SetCell(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String, clumnNo As Integer)
204
192
  Select Case cellDataD
205
193
  Case 100
206
- Cells(count, 6).Value = str1
194
+ Cells(count, clumnNo).Value = str1
207
195
  Case 1 To 99
208
- Cells(count, 6).Value = str2
196
+ Cells(count, clumnNo).Value = str2
209
197
  Case 0
210
- Cells(count, 6).Value = str3
198
+ Cells(count, clumnNo).Value = str3
211
199
  End Select
212
- End Sub
200
+ End Sub```
213
- ```

3

改修後コードを追加しました。

2017/09/28 09:19

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -129,4 +129,85 @@
129
129
  '処理
130
130
  ElseIf
131
131
  ```
132
- とした後のElseIfの式はどうかきますか?
132
+ とした後のElseIfの式はどうかきますか?
133
+
134
+ BA後の改修したコード
135
+ ```vba
136
+ 'Eセルにデータセットするための分岐条件
137
+ Sub ECellSetter()
138
+ For i = 6 To rowsCount
139
+ '作業着手予定日が空ならFor文から抜ける
140
+ If Cells(i, 2).Value = "" Then
141
+ Exit For
142
+ End If
143
+ 'B6セルから末端までのデータを一時的に格納する。
144
+ cellDateB = Cells(i, 2).Value
145
+ 'D6セルから末端までのデータを一時的に格納する。
146
+ cellDataD = Cells(i, 4).Value
147
+
148
+ If cellDateB < cellDateFrom Then
149
+ Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
150
+ ElseIf cellDateB = cellDateFrom Then
151
+ Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
152
+ ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo Then
153
+ Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
154
+ ElseIf cellDateB = cellDateTo Then
155
+ Call SetE(i, cellDataD, C_START, C_START, C_START_LATE)
156
+ ElseIf cellDateTo < cellDateB Then
157
+ Call SetE(i, cellDataD, C_START_FIRST, C_START_FIRST, C_START_EMP)
158
+ End If
159
+
160
+ Next i
161
+ End Sub
162
+
163
+ 'Eセルにデータセットする。
164
+ Sub SetE(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String)
165
+ Select Case cellDataD
166
+ Case 100
167
+ Cells(count, 5).Value = str1
168
+ Case 1 To 99
169
+ Cells(count, 5).Value = str2
170
+ Case 0
171
+ Cells(count, 5).Value = str3
172
+ End Select
173
+ End Sub
174
+
175
+
176
+ Sub FCellSetter()
177
+ For i = 6 To rowsCount
178
+ '作業着手予定日が空ならFor文から抜ける
179
+ If Cells(i, 2).Value = "" Then
180
+ Exit For
181
+ End If
182
+ 'C6セルから末端までのデータを一時的に格納する。
183
+ cellDateC = Cells(i, 3).Value
184
+ 'D6セルから末端までのデータを一時的に格納する。
185
+ cellDataD = Cells(i, 4).Value
186
+
187
+ If cellDateC < cellDateFrom Then
188
+ Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
189
+ ElseIf cellDateC = cellDateFrom Then
190
+ Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
191
+ ElseIf cellDateFrom < cellDateC And cellDateC < cellDateTo Then
192
+ Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
193
+ ElseIf cellDateC = cellDateTo Then
194
+ Call SetF(i, cellDataD, C_END, C_END_LATE, C_END_LATE)
195
+ ElseIf cellDateTo < cellDateC Then
196
+ Call SetF(i, cellDataD, C_END_FIRST, C_END_EMP, C_END_EMP)
197
+ End If
198
+
199
+ Next i
200
+ End Sub
201
+
202
+ 'Fセルにデータセットする。
203
+ Sub SetF(count As Variant, cellDataD As Integer, str1 As String, str2 As String, str3 As String)
204
+ Select Case cellDataD
205
+ Case 100
206
+ Cells(count, 6).Value = str1
207
+ Case 1 To 99
208
+ Cells(count, 6).Value = str2
209
+ Case 0
210
+ Cells(count, 6).Value = str3
211
+ End Select
212
+ End Sub
213
+ ```

2

誤字修正

2017/09/28 07:22

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -129,4 +129,4 @@
129
129
  '処理
130
130
  ElseIf
131
131
  ```
132
- とした後のElseIf式はどうかきますか?
132
+ とした後のElseIf式はどうかきますか?

1

追加説明

2017/09/28 05:17

投稿

King_of_Flies
King_of_Flies

スコア382

title CHANGED
File without changes
body CHANGED
@@ -56,4 +56,77 @@
56
56
  こうなった場合、皆様はどんな工夫を凝らして、
57
57
  スマートなコードを書いていますか。
58
58
 
59
- 教えてください。
59
+ 教えてください。
60
+
61
+
62
+ 回答をいただいたので例文追加で、説明を求めます。
63
+ ```vba
64
+ Sub CellSetter()
65
+ For i = 6 To rowsCount
66
+ '作業着手予定日が空ならFor文から抜ける
67
+ If Cells(i, 2).Value = "" Then
68
+ Exit For
69
+ End If
70
+ 'B6セルから末端までのデータを一時的に格納する。
71
+ cellDateB = Cells(i, 2).Value
72
+ 'C6セルから末端までのデータを一時的に格納する。
73
+ cellDateC = Cells(i, 3).Value
74
+ 'D6セルから末端までのデータを一時的に格納する。
75
+ cellDataD = Cells(i, 4).Value
76
+ '①
77
+ If cellDateC < cellDateFrom Then
78
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
79
+ '②
80
+ ElseIf cellDateB < cellDateFrom And cellDateC = cellDateTo Then
81
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
82
+ '③
83
+ ElseIf cellDateB < cellDateFrom And cellDateFrom < cellDateC And cellDateC < cellDateTo Then
84
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
85
+ '④
86
+ ElseIf cellDateFrom = cellDateB And cellDateFrom < cellDateC And cellDateC < cellDateTo Then
87
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
88
+ '⑤
89
+ ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo And cellDateFrom < cellDateC And cellDateC < cellDateTo Then
90
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
91
+ '⑥
92
+ ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo And cellDateC = cellDateTo Then
93
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
94
+ '⑦
95
+ ElseIf cellDateFrom < cellDateB And cellDateB < cellDateTo And cellDateTo < cellDateC Then
96
+ Call Selecter(i, cellDataD, C_START, C_END_FIRST, C_START, C_END_EMP, C_START_LATE, C_END_EMP)
97
+ '⑧
98
+ ElseIf cellDateB = cellDateTo And cellDateTo < cellDateC Then
99
+ Call Selecter(i, cellDataD, C_START, C_END_FIRST, C_START, C_END_EMP, C_START_LATE, C_END_EMP)
100
+ '⑨
101
+ ElseIf cellDateTo < cellDateB Then
102
+ Call Selecter(i, cellDataD, C_START_FIRST, C_END_FIRST, C_START_FIRST, C_END_EMP, C_START_EMP, C_END_EMP)
103
+ '⑩
104
+ ElseIf cellDateB < cellDateFrom And cellDateFrom = cellDateC Then
105
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
106
+ '⑪
107
+ ElseIf cellDateB = cellDateFrom And cellDateC = cellDateTo Then
108
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
109
+ '⑫
110
+ ElseIf cellDateB = cellDateFrom And cellDateTo < cellDateC Then
111
+ Call Selecter(i, cellDataD, C_START, C_END_FIRST, C_START, C_END_EMP, C_START_LATE, C_END_EMP)
112
+ '⑬
113
+ ElseIf cellDateB < cellDateFrom And cellDateTo < cellDateC Then
114
+ Call Selecter(i, cellDataD, C_START, C_END_FIRST, C_START, C_END_EMP, C_START_LATE, C_END_EMP)
115
+ '⑭
116
+ ElseIf cellDateB = cellDateFrom And cellDateC = cellDateFrom Then
117
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
118
+ '⑮
119
+ ElseIf cellDateB = cellDateTo And cellDateC = cellDateTo Then
120
+ Call Selecter(i, cellDataD, C_START, C_END, C_START, C_END_LATE, C_START_LATE, C_END_LATE)
121
+ End If
122
+ Next i
123
+ End Sub
124
+ ```
125
+
126
+ このプログラムで分岐結果が少ないのは⑨の一個ですが、
127
+ ```vba
128
+ If cellDateTo < cellDateB Then
129
+ '処理
130
+ ElseIf
131
+ ```
132
+ とした後のElseIfそ式はどうかきますか?