回答編集履歴

2

修正

2017/03/01 05:25

投稿

jawa
jawa

スコア3013

test CHANGED
@@ -238,7 +238,7 @@
238
238
 
239
239
 
240
240
 
241
- '締月の最終日を算出(翌月1日 -1日の日付)
241
+ '締月の最終日を算出((締月の翌月1日-1日の日付)
242
242
 
243
243
  dayLast = Day(DateAdd("d", -1, DateAdd("m", 1, dayCutoff)))
244
244
 

1

追記

2017/03/01 05:25

投稿

jawa
jawa

スコア3013

test CHANGED
@@ -141,3 +141,297 @@
141
141
 
142
142
 
143
143
  頑張ってみてください。
144
+
145
+
146
+
147
+ 追記(サンプルコード)
148
+
149
+ ---
150
+
151
+ ```
152
+
153
+ Option Explicit
154
+
155
+ Public wsData As Worksheet '「請求データ」シートを入れるオブジェクト変数
156
+
157
+ Public wsTemplate As Worksheet '★「請求書ひな形」シートを入れるオブジェクト変数
158
+
159
+ Public wsInvoice As Worksheet '★「請求書」シートを入れるオブジェクト変数
160
+
161
+ Public wsClient As Worksheet '「取引先一覧」シートを入れるオブジェクト変数
162
+
163
+ Public rowsData As Long '「請求データ」の行数
164
+
165
+ Public rowsClient As Long '「取引先一覧」の行数
166
+
167
+
168
+
169
+ Sub シート初期化()
170
+
171
+
172
+
173
+ Set wsData = ThisWorkbook.Worksheets("納品データ")
174
+
175
+ Set wsTemplate = ThisWorkbook.Worksheets("請求書雛形") '★「請求書ひな形」シート
176
+
177
+ Set wsInvoice = ThisWorkbook.Worksheets("請求書雛形")
178
+
179
+ Set wsClient = ThisWorkbook.Worksheets("取引先一覧")
180
+
181
+
182
+
183
+ rowsData = wsData.Cells(Rows.Count, 2).End(xlUp).Row '最後の行数を取得
184
+
185
+ rowsClient = wsClient.Cells(Rows.Count, 1).End(xlUp).Row '「取引先マスタ」の最後の行数を取得
186
+
187
+
188
+
189
+ wsTemplate.Rows("21:50").Hidden = False '隠れている行を再表示する
190
+
191
+ wsTemplate.Rows("44:47").Hidden = False '隠れている行を再表示する
192
+
193
+
194
+
195
+ wsTemplate.Range("B22:D41").ClearContents 'ここに転記先のセル範囲のクリアを入れる
196
+
197
+
198
+
199
+ End Sub
200
+
201
+
202
+
203
+ Sub 日別_請求書作成()
204
+
205
+ Dim j As Long, i As Long, n As Long, m As Long, b As Long, c As Long, k As Long, h As Long, p As Long, f As Long
206
+
207
+
208
+
209
+ Dim dayData As Date '納品日格納用変数
210
+
211
+ Dim dayCutoff As Date '締月入力用変数
212
+
213
+ Dim strClient As String '取引先格納用変数
214
+
215
+ Dim g As Long
216
+
217
+
218
+
219
+ Dim dayLoop As Integer '日付ループカウンタ
220
+
221
+ Dim dayLast As Integer '締月の最終日
222
+
223
+ Dim dayPickup As Date '処理対象の日付
224
+
225
+
226
+
227
+
228
+
229
+ dayCutoff = InputBox("締月を入力してください(例:2016/9)")
230
+
231
+
232
+
233
+ Call シート初期化
234
+
235
+
236
+
237
+ p = 1
238
+
239
+
240
+
241
+ '締月の最終日を算出(翌月1日 -1日の日付)
242
+
243
+ dayLast = Day(DateAdd("d", -1, DateAdd("m", 1, dayCutoff)))
244
+
245
+
246
+
247
+ For h = 2 To rowsClient
248
+
249
+ For dayLoop = 1 To dayLast '★日付ループここから
250
+
251
+ dayPickup = DateAdd("d", dayLoop - 1, dayCutoff) '★対象日付の生成
252
+
253
+
254
+
255
+ If WorksheetFunction.SumIfs( _
256
+
257
+ wsData.Range("G:G"), _
258
+
259
+ wsData.Range("H:H"), "", _
260
+
261
+ wsData.Range("A:A"), "=" & dayPickup, _
262
+
263
+ wsData.Range("B:B"), wsClient.Cells(h, 1).Value _
264
+
265
+ ) > 0 Then '日付指定で対象データ有無を判定
266
+
267
+
268
+
269
+ Workbooks.Add '新規ワークブックを作成
270
+
271
+ wsTemplate.Copy before:=ActiveWorkbook.Sheets(1) '新規ワークブックのsheet1の前にひな形をコピー
272
+
273
+
274
+
275
+ Set wsInvoice = ActiveSheet 'コピーしたシートを変数にセット
276
+
277
+ wsInvoice.Name = "請求書" 'シート名を変更
278
+
279
+
280
+
281
+ n = 1
282
+
283
+ c = 0
284
+
285
+ k = 0
286
+
287
+ m = 2
288
+
289
+
290
+
291
+ For i = 2 To rowsData
292
+
293
+
294
+
295
+ dayData = wsData.Cells(m, 1).Value '現在の行の納品日を取得
296
+
297
+ strClient = wsData.Cells(m, 2).Value
298
+
299
+
300
+
301
+ If wsData.Cells(m, 8).Value = "" Then
302
+
303
+ If strClient = wsClient.Cells(h, 1).Value Then
304
+
305
+ If Year(dayData) = Year(dayPickup) And _
306
+
307
+ Month(dayData) = Month(dayPickup) And _
308
+
309
+ Day(dayData) = Day(dayPickup) Then '★対象日と年月日が同じならデータ出力
310
+
311
+
312
+
313
+ For b = 2 To 5
314
+
315
+ '納品データを転記
316
+
317
+ wsInvoice.Cells(22 + k + n, b - c).Value = wsData.Cells(i, b + 1).Value
318
+
319
+ c = 1
320
+
321
+ n = 0
322
+
323
+ Next b
324
+
325
+ n = 1
326
+
327
+ k = k + 2
328
+
329
+ c = 0
330
+
331
+ If wsData.Cells(i, 8).Value = "" Then
332
+
333
+ wsData.Cells(i, 8).Value = Format(Date, "yyyymmdd")
334
+
335
+ End If
336
+
337
+
338
+
339
+ End If
340
+
341
+ End If
342
+
343
+ End If
344
+
345
+
346
+
347
+ m = m + 1
348
+
349
+
350
+
351
+ Next i
352
+
353
+
354
+
355
+ wsInvoice.Rows(22 + k & ":41").Hidden = True 'データがない行を隠す
356
+
357
+
358
+
359
+ wsInvoice.Cells(8, 2).Value = wsClient.Cells(h, 2).Value
360
+
361
+ wsInvoice.Cells(9, 2).Value = wsClient.Cells(h, 3).Value
362
+
363
+ wsInvoice.Cells(10, 2).Value = wsClient.Cells(h, 4).Value
364
+
365
+ wsInvoice.Cells(11, 2).Value = wsClient.Cells(h, 1).Value
366
+
367
+
368
+
369
+ wsInvoice.Range("E1").Value = "請求番号:" & (Format(Date, "yyyymmdd")) & "-" & (Format(p, "000"))
370
+
371
+
372
+
373
+ If IsEmpty(wsClient.Cells(h, 5)) Then '源泉徴収税のセルが不要ならば
374
+
375
+ wsInvoice.Rows(44 & ":47").Hidden = True '源泉徴収税、差引額のセルを消す
376
+
377
+ wsInvoice.Range("B16").Value = "ご請求金額 ¥" & Format(wsInvoice.Cells(42, 5).Value, "#,###,##0") & " -"
378
+
379
+ Else
380
+
381
+ wsInvoice.Range("B16").Value = "ご請求金額 ¥" & Format(wsInvoice.Cells(46, 5).Value, "#,###,##0") & " -"
382
+
383
+ End If
384
+
385
+
386
+
387
+ Dim strFile As String '保存先フォルダパス&ファイル名(拡張子抜き)
388
+
389
+
390
+
391
+ '★出力ファイル名(納品日を追加)
392
+
393
+ strFile = ThisWorkbook.Path & "\" & wsClient.Cells(h, 1).Value & "_" & (Format(Date, "yyyymmdd")) & "_" & (Format(dayPickup, "yyyymmdd")) & "_" & (Format(p, "000"))
394
+
395
+ wsInvoice.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile & ".pdf" '選択したシートをPDF出力
396
+
397
+
398
+
399
+ '*PDF出力設定*
400
+
401
+ With wsInvoice.PageSetup
402
+
403
+ .Zoom = False '倍率をクリア
404
+
405
+ .FitToPagesWide = 1 '横方向に1ページに収める
406
+
407
+ .FitToPagesTall = 1 '縦方向に1ページに収める
408
+
409
+ .CenterHorizontally = True '水平方向に中央配置
410
+
411
+ .TopMargin = Application.CentimetersToPoints(1) '上マージンを1cm
412
+
413
+ .BottomMargin = Application.CentimetersToPoints(1) '下マージンを1cm
414
+
415
+ End With
416
+
417
+
418
+
419
+ wsInvoice.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile & ".pdf" '選択したシートをPDF出力
420
+
421
+ ActiveWorkbook.Close savechanges:=True, Filename:=strFile & ".xlsx" 'アクティブブックを名前を付けて保存して閉じる
422
+
423
+
424
+
425
+ p = p + 1
426
+
427
+ End If
428
+
429
+ Next dayLoop '★日付ループここまで
430
+
431
+ Next h
432
+
433
+ MsgBox "完了"
434
+
435
+ End Sub
436
+
437
+ ```