質問編集履歴

1

全体のソースコードを追記しました

2019/04/23 00:36

投稿

Windshower0704
Windshower0704

スコア10

test CHANGED
@@ -1 +1 @@
1
- 実行時エラー 1004 "*****(対象ファイル名)"にアクセスできません。対応方法
1
+ 実行時エラー 1004 "*****(対象ファイル名)"にアクセスできません。対応方法をご教示ください。
test CHANGED
@@ -20,23 +20,13 @@
20
20
 
21
21
  ```
22
22
 
23
-
24
-
25
23
  実行時エラー 1004
26
24
 
27
-
28
-
29
25
  "*****(対象ファイル名)"にアクセスできません。
30
26
 
31
-
32
-
33
- ```
27
+ ```
34
-
35
-
36
-
28
+
37
- ### 該当のソースコード(VBA)
29
+ 問題になっている該当のソースコード(VBA)
38
-
39
-
40
30
 
41
31
  ```
42
32
 
@@ -46,6 +36,428 @@
46
36
 
47
37
  ```
48
38
 
39
+ 全体のソースコード
40
+
41
+ ```
42
+
43
+
44
+
45
+ Option Explicit
46
+
47
+ Dim lOptionButton As Long
48
+
49
+ Sub OptionButton1_Click()
50
+
51
+ lOptionButton = 1
52
+
53
+ End Sub
54
+
55
+ Sub OptionButton2_Click()
56
+
57
+ lOptionButton = 2
58
+
59
+ End Sub
60
+
61
+ Function GetOptionButton() As Long
62
+
63
+ Dim lCnt As Long
64
+
65
+ Dim lTarget As Long
66
+
67
+ For lCnt = 1 To 2
68
+
69
+ lTarget = ActiveSheet.OptionButtons("Option Button " & lCnt).Value
70
+
71
+ If lTarget = 1 Then
72
+
73
+ GetOptionButton = lCnt
74
+
75
+ Exit Function
76
+
77
+ End If
78
+
79
+ Next
80
+
81
+ GetOptionButton = 1
82
+
83
+ End Function
84
+
85
+
86
+
87
+ Sub CallListMake()
88
+
89
+ Dim lStatusCnt As Long
90
+
91
+ Dim strStatusName() As String
92
+
93
+ strStatusName = Split("審査依頼済,審査保留", ",")
94
+
95
+
96
+
97
+ Dim bErrFlg As Boolean
98
+
99
+ Dim bCompany As Boolean
100
+
101
+
102
+
103
+ Dim strInputFolder As String
104
+
105
+ Dim strOutputFolder As String
106
+
107
+
108
+
109
+ Dim strInputFileName As String
110
+
111
+ Dim strFormatFileName As String
112
+
113
+ Dim strOutputFileName As String
114
+
115
+
116
+
117
+ Dim lInputStartRow As Long
118
+
119
+ Dim lOutputStartRow As Long
120
+
121
+
122
+
123
+ Dim lInputCntRow As Long
124
+
125
+ Dim lOutputCntRow As Long
126
+
127
+
128
+
129
+ Dim lInputMaxRow As Long
130
+
131
+ Dim strFormatBookName As String
132
+
133
+ Dim strInputBookName As String
134
+
135
+
136
+
137
+ Dim lCallListFileNo As Long
138
+
139
+ Dim lCallListMaxRow As Long
140
+
141
+
142
+
143
+ strInputFolder = Range("K5").Value
144
+
145
+ strOutputFolder = Range("K6").Value
146
+
147
+ strFormatFileName = Range("K7").Value
148
+
149
+ lCallListMaxRow = Range("K8").Value
150
+
151
+ lOptionButton = GetOptionButton
152
+
153
+
154
+
155
+ lInputStartRow = 2
156
+
157
+ lOutputStartRow = 2
158
+
159
+
160
+
161
+ '・申込・契約情報_yyyymmddhhssMM.cxvを開く・・以下 InputFile
162
+
163
+ ' →最初はファイルを手動で読込でリリース、架電リスト作成ツールができたら最新1ファイルを開くに仕様変更する
164
+
165
+ If lOptionButton = 1 Then
166
+
167
+ strInputFileName = Dir(strInputFolder & "\" & "*.csv", vbNormal)
168
+
169
+ If strInputFileName = "" Then
170
+
171
+ GoTo END_LABEL:
172
+
173
+ End If
174
+
175
+ Workbooks.Open Filename:=strInputFolder & "\" & strInputFileName
176
+
177
+ Else
178
+
179
+ ChDrive Left(strInputFolder, 3)
180
+
181
+ ChDir strInputFolder
182
+
183
+ Call FileOpen("CSVファイル(*.csv?),*.csv?", 1, "申込・契約情報_*.csvを開く", False, "申込・契約情報_*.csv", "申込・契約情報", bErrFlg)
184
+
185
+ If bErrFlg Then
186
+
187
+ GoTo END_LABEL:
188
+
189
+ End If
190
+
191
+ End If
192
+
193
+ strInputBookName = ActiveWorkbook.Name
194
+
195
+ lInputMaxRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
196
+
197
+
198
+
199
+ ' 申込番号で昇順にソート Add 2018/03/16 H.Miki
200
+
201
+ Dim lInputMaxCol As Long
202
+
203
+ lInputMaxCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
204
+
205
+ Workbooks(strInputBookName).Worksheets(1).Range(Workbooks(strInputBookName).Worksheets(1).Cells(2, 1), Workbooks(strInputBookName).Worksheets(1).Cells(lInputMaxRow, lInputMaxCol)).Sort _
206
+
207
+ Key1:=Workbooks(strInputBookName).Worksheets(1).Cells(1, 1), order1:=xlAscending, Header:=xlNo, DataOption1:=xlSortTextAsNumbers
208
+
209
+
210
+
211
+ ' 画面の更新を停止、不要な画面描画を抑止する
212
+
213
+ Application.ScreenUpdating = False
214
+
215
+ For lStatusCnt = LBound(strStatusName) To UBound(strStatusName) ' 配列の各要素を順に処理
216
+
217
+ '・「\web申込入居審査」の「03_架電リストフォーマット.xlsx」を開く・・以下 FormatFile
218
+
219
+ Workbooks.Open Filename:=strFormatFileName
220
+
221
+ strFormatBookName = ActiveWorkbook.Name
222
+
223
+ Workbooks(strInputBookName).Activate
224
+
225
+
226
+
227
+ '・InputFileの申込・契約ステータスが審査依頼済の行の以下列をFormatFileに張り付け
228
+
229
+ ' →申込番号、申込・契約ステータス、個人/法人、契約者氏名、契約者氏名カナ、商品、契約時条件
230
+
231
+ '・InputFileの最終行まで繰り返し
232
+
233
+ lOutputCntRow = lOutputStartRow
234
+
235
+ lCallListFileNo = 1
236
+
237
+ For lInputCntRow = lInputStartRow To lInputMaxRow
238
+
239
+ ' 申込受付日が現在よりも2か月以内で無い場合は読み飛ばし Add 2018/03/16 H.Miki
240
+
241
+ If Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 9).Value < DateAdd("m", -2, Now) Then GoTo Continue:
242
+
243
+
244
+
245
+ If Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 3).Value = strStatusName(lStatusCnt) Then
246
+
247
+ bCompany = False
248
+
249
+ If Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 4).Value = "法人" Then ' 法人の場合は法人名が名前に入る 編集2018/05/7 horiuchi
250
+
251
+ bCompany = True
252
+
253
+ End If
254
+
255
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 1).Value = ""
256
+
257
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 2).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 1).Value '. 申込番号 編集2018/05/7 horiuchi
258
+
259
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 3).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 24).Value '. 担当者氏名 編集2018/07/10 horiuchi
260
+
261
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 4).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 9).Value '. 申込受付日 編集2018/05/7 horiuchi
262
+
263
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 5).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 78).Value '. 商品
264
+
265
+ If bCompany = False Then
266
+
267
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 6).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 102).Value '.【個人】氏名 編集2018/05/7 horiuchi
268
+
269
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 7).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 103).Value '.【個人】氏名(カナ) 編集2018/05/7 horiuchi
270
+
271
+ Else
272
+
273
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 6).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 153).Value '.【法人】会社名 編集2018/05/7 horiuchi
274
+
275
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 7).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 154).Value '.【法人】会社名(カナ) 編集2018/05/7 horiuchi
276
+
277
+ End If
278
+
279
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 8).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 16).Value '.契約時条件 編集2018/05/7 horiuchi
280
+
281
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 9).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 25).Value '.(基本情報)備考 編集2018/05/7 horiuchi
282
+
283
+ Workbooks(strFormatBookName).Worksheets(1).Cells(lOutputCntRow, 10).Value = Workbooks(strInputBookName).Worksheets(1).Cells(lInputCntRow, 4).Value '.個人/法人 編集2019/03/12horiuchi
284
+
285
+ lOutputCntRow = lOutputCntRow + 1
286
+
287
+
288
+
289
+ '・1ファイルあたりの件数に達した場合、FormatFileを「\web申込入居審査\03_架電リスト\審査依頼済」に名前を付けて保存・・以下 OutputFile
290
+
291
+ If (lOutputCntRow - lOutputStartRow) >= lCallListMaxRow Then
292
+
293
+ Application.DisplayAlerts = False
294
+
295
+ strOutputFileName = "架電リスト_" & strStatusName(lStatusCnt) & "_" & Format(Now, "yyyymmdd") & "_" & Format(lCallListFileNo, "000") & ".xlsx"
296
+
297
+ Workbooks(strFormatBookName).SaveAs Filename:=strOutputFolder & "\" & strStatusName(lStatusCnt) & "\" & strOutputFileName, FileFormat:=xlWorkbookDefault
298
+
299
+ Workbooks(strOutputFileName).Close savechanges:=False
300
+
301
+ Workbooks.Open Filename:=strFormatFileName
302
+
303
+ Workbooks(strInputBookName).Activate
304
+
305
+ lOutputCntRow = lOutputStartRow
306
+
307
+ lCallListFileNo = lCallListFileNo + 1
308
+
309
+ Application.DisplayAlerts = True
310
+
311
+ End If
312
+
313
+ End If
314
+
315
+ Continue:
316
+
317
+ Next
318
+
319
+ Application.DisplayAlerts = False
320
+
321
+ strOutputFileName = "架電リスト_" & strStatusName(lStatusCnt) & "_" & Format(Now, "yyyymmdd") & "_" & Format(lCallListFileNo, "000") & ".xlsx"
322
+
323
+ Workbooks(strFormatBookName).SaveAs Filename:=strOutputFolder & "\" & strStatusName(lStatusCnt) & "\" & strOutputFileName, FileFormat:=xlWorkbookDefault
324
+
325
+ Workbooks(strOutputFileName).Close savechanges:=False
326
+
327
+ Application.DisplayAlerts = True
328
+
329
+ Next
330
+
331
+ Application.DisplayAlerts = False
332
+
333
+ Workbooks(strInputBookName).Close savechanges:=False
334
+
335
+ Application.DisplayAlerts = True
336
+
337
+ END_LABEL:
338
+
339
+ Application.ScreenUpdating = True
340
+
341
+ End Sub
342
+
343
+ Sub FileOpen(WkFileFilter, WkFilterIndex, WkTitle, WkMultiSelect, WkFilename, WkErr, bErrFlg)
344
+
345
+
346
+
347
+ Dim OpenFileName As String
348
+
349
+ Dim WKBook_Name As String
350
+
351
+ Dim PathName As String
352
+
353
+ Dim pos As Long
354
+
355
+ Dim flag As Boolean
356
+
357
+ Dim wb As Workbook
358
+
359
+
360
+
361
+ bErrFlg = False
362
+
363
+
364
+
365
+ ' ダイアログを開く
366
+
367
+ OpenFileName = Application.GetOpenFilename( _
368
+
369
+ FileFilter:=WkFileFilter _
370
+
371
+ , FilterIndex:=WkFilterIndex _
372
+
373
+ , Title:=WkTitle _
374
+
375
+ , MultiSelect:=WkMultiSelect _
376
+
377
+ )
378
+
379
+
380
+
381
+ pos = InStrRev(OpenFileName, "\")
382
+
383
+ PathName = Left(OpenFileName, pos)
384
+
385
+ WKBook_Name = Mid(OpenFileName, pos + 1)
386
+
387
+
388
+
389
+ flag = False
390
+
391
+
392
+
393
+ ' 現在開いているワークブックを検索
394
+
395
+ For Each wb In Workbooks
396
+
397
+ ' 開いているワークブックが取得したファイル名と一致
398
+
399
+ If wb.FullName = OpenFileName Then
400
+
401
+ flag = True
402
+
403
+ Exit For
404
+
405
+ End If
406
+
407
+ Next wb
408
+
409
+
410
+
411
+
412
+
413
+ If flag = True Then
414
+
415
+ bErrFlg = True
416
+
417
+ MsgBox OpenFileName & "は既に開いているため閉じてください。"
418
+
419
+ GoTo END_LABEL:
420
+
421
+ End If
422
+
423
+
424
+
425
+ If OpenFileName <> "False" Then
426
+
427
+ If WKBook_Name Like WkFilename Then
428
+
429
+ Workbooks.Open OpenFileName
430
+
431
+ Else
432
+
433
+ bErrFlg = True
434
+
435
+ MsgBox WkErr & "ファイルが選択されていません。"
436
+
437
+ GoTo END_LABEL:
438
+
439
+ End If
440
+
441
+ Else
442
+
443
+ bErrFlg = True
444
+
445
+ MsgBox WkErr & "ファイルを選択してください。"
446
+
447
+ GoTo END_LABEL:
448
+
449
+ End If
450
+
451
+
452
+
453
+ END_LABEL:
454
+
455
+
456
+
457
+ End Sub
458
+
459
+ ```
460
+
49
461
 
50
462
 
51
463
  対応方法をご教示ください、よろしくお願いいたします。