回答編集履歴
2
修正
answer
CHANGED
@@ -118,7 +118,7 @@
|
|
118
118
|
|
119
119
|
p = 1
|
120
120
|
|
121
|
-
'締月の最終日を算出(翌月1日
|
121
|
+
'★締月の最終日を算出((締月の翌月1日)-1日の日付)
|
122
122
|
dayLast = Day(DateAdd("d", -1, DateAdd("m", 1, dayCutoff)))
|
123
123
|
|
124
124
|
For h = 2 To rowsClient
|
1
追記
answer
CHANGED
@@ -69,4 +69,151 @@
|
|
69
69
|
|
70
70
|
細かい部分ですが、シート名も得意先単位ではなく日単位となるよう考慮が必要でしょう。
|
71
71
|
|
72
|
-
頑張ってみてください。
|
72
|
+
頑張ってみてください。
|
73
|
+
|
74
|
+
追記(サンプルコード)
|
75
|
+
---
|
76
|
+
```
|
77
|
+
Option Explicit
|
78
|
+
Public wsData As Worksheet '「請求データ」シートを入れるオブジェクト変数
|
79
|
+
Public wsTemplate As Worksheet '★「請求書ひな形」シートを入れるオブジェクト変数
|
80
|
+
Public wsInvoice As Worksheet '★「請求書」シートを入れるオブジェクト変数
|
81
|
+
Public wsClient As Worksheet '「取引先一覧」シートを入れるオブジェクト変数
|
82
|
+
Public rowsData As Long '「請求データ」の行数
|
83
|
+
Public rowsClient As Long '「取引先一覧」の行数
|
84
|
+
|
85
|
+
Sub シート初期化()
|
86
|
+
|
87
|
+
Set wsData = ThisWorkbook.Worksheets("納品データ")
|
88
|
+
Set wsTemplate = ThisWorkbook.Worksheets("請求書雛形") '★「請求書ひな形」シート
|
89
|
+
Set wsInvoice = ThisWorkbook.Worksheets("請求書雛形")
|
90
|
+
Set wsClient = ThisWorkbook.Worksheets("取引先一覧")
|
91
|
+
|
92
|
+
rowsData = wsData.Cells(Rows.Count, 2).End(xlUp).Row '最後の行数を取得
|
93
|
+
rowsClient = wsClient.Cells(Rows.Count, 1).End(xlUp).Row '「取引先マスタ」の最後の行数を取得
|
94
|
+
|
95
|
+
wsTemplate.Rows("21:50").Hidden = False '隠れている行を再表示する
|
96
|
+
wsTemplate.Rows("44:47").Hidden = False '隠れている行を再表示する
|
97
|
+
|
98
|
+
wsTemplate.Range("B22:D41").ClearContents 'ここに転記先のセル範囲のクリアを入れる
|
99
|
+
|
100
|
+
End Sub
|
101
|
+
|
102
|
+
Sub 日別_請求書作成()
|
103
|
+
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
|
104
|
+
|
105
|
+
Dim dayData As Date '納品日格納用変数
|
106
|
+
Dim dayCutoff As Date '締月入力用変数
|
107
|
+
Dim strClient As String '取引先格納用変数
|
108
|
+
Dim g As Long
|
109
|
+
|
110
|
+
Dim dayLoop As Integer '日付ループカウンタ
|
111
|
+
Dim dayLast As Integer '締月の最終日
|
112
|
+
Dim dayPickup As Date '処理対象の日付
|
113
|
+
|
114
|
+
|
115
|
+
dayCutoff = InputBox("締月を入力してください(例:2016/9)")
|
116
|
+
|
117
|
+
Call シート初期化
|
118
|
+
|
119
|
+
p = 1
|
120
|
+
|
121
|
+
'締月の最終日を算出(翌月1日 -1日の日付)
|
122
|
+
dayLast = Day(DateAdd("d", -1, DateAdd("m", 1, dayCutoff)))
|
123
|
+
|
124
|
+
For h = 2 To rowsClient
|
125
|
+
For dayLoop = 1 To dayLast '★日付ループここから
|
126
|
+
dayPickup = DateAdd("d", dayLoop - 1, dayCutoff) '★対象日付の生成
|
127
|
+
|
128
|
+
If WorksheetFunction.SumIfs( _
|
129
|
+
wsData.Range("G:G"), _
|
130
|
+
wsData.Range("H:H"), "", _
|
131
|
+
wsData.Range("A:A"), "=" & dayPickup, _
|
132
|
+
wsData.Range("B:B"), wsClient.Cells(h, 1).Value _
|
133
|
+
) > 0 Then '日付指定で対象データ有無を判定
|
134
|
+
|
135
|
+
Workbooks.Add '新規ワークブックを作成
|
136
|
+
wsTemplate.Copy before:=ActiveWorkbook.Sheets(1) '新規ワークブックのsheet1の前にひな形をコピー
|
137
|
+
|
138
|
+
Set wsInvoice = ActiveSheet 'コピーしたシートを変数にセット
|
139
|
+
wsInvoice.Name = "請求書" 'シート名を変更
|
140
|
+
|
141
|
+
n = 1
|
142
|
+
c = 0
|
143
|
+
k = 0
|
144
|
+
m = 2
|
145
|
+
|
146
|
+
For i = 2 To rowsData
|
147
|
+
|
148
|
+
dayData = wsData.Cells(m, 1).Value '現在の行の納品日を取得
|
149
|
+
strClient = wsData.Cells(m, 2).Value
|
150
|
+
|
151
|
+
If wsData.Cells(m, 8).Value = "" Then
|
152
|
+
If strClient = wsClient.Cells(h, 1).Value Then
|
153
|
+
If Year(dayData) = Year(dayPickup) And _
|
154
|
+
Month(dayData) = Month(dayPickup) And _
|
155
|
+
Day(dayData) = Day(dayPickup) Then '★対象日と年月日が同じならデータ出力
|
156
|
+
|
157
|
+
For b = 2 To 5
|
158
|
+
'納品データを転記
|
159
|
+
wsInvoice.Cells(22 + k + n, b - c).Value = wsData.Cells(i, b + 1).Value
|
160
|
+
c = 1
|
161
|
+
n = 0
|
162
|
+
Next b
|
163
|
+
n = 1
|
164
|
+
k = k + 2
|
165
|
+
c = 0
|
166
|
+
If wsData.Cells(i, 8).Value = "" Then
|
167
|
+
wsData.Cells(i, 8).Value = Format(Date, "yyyymmdd")
|
168
|
+
End If
|
169
|
+
|
170
|
+
End If
|
171
|
+
End If
|
172
|
+
End If
|
173
|
+
|
174
|
+
m = m + 1
|
175
|
+
|
176
|
+
Next i
|
177
|
+
|
178
|
+
wsInvoice.Rows(22 + k & ":41").Hidden = True 'データがない行を隠す
|
179
|
+
|
180
|
+
wsInvoice.Cells(8, 2).Value = wsClient.Cells(h, 2).Value
|
181
|
+
wsInvoice.Cells(9, 2).Value = wsClient.Cells(h, 3).Value
|
182
|
+
wsInvoice.Cells(10, 2).Value = wsClient.Cells(h, 4).Value
|
183
|
+
wsInvoice.Cells(11, 2).Value = wsClient.Cells(h, 1).Value
|
184
|
+
|
185
|
+
wsInvoice.Range("E1").Value = "請求番号:" & (Format(Date, "yyyymmdd")) & "-" & (Format(p, "000"))
|
186
|
+
|
187
|
+
If IsEmpty(wsClient.Cells(h, 5)) Then '源泉徴収税のセルが不要ならば
|
188
|
+
wsInvoice.Rows(44 & ":47").Hidden = True '源泉徴収税、差引額のセルを消す
|
189
|
+
wsInvoice.Range("B16").Value = "ご請求金額 ¥" & Format(wsInvoice.Cells(42, 5).Value, "#,###,##0") & " -"
|
190
|
+
Else
|
191
|
+
wsInvoice.Range("B16").Value = "ご請求金額 ¥" & Format(wsInvoice.Cells(46, 5).Value, "#,###,##0") & " -"
|
192
|
+
End If
|
193
|
+
|
194
|
+
Dim strFile As String '保存先フォルダパス&ファイル名(拡張子抜き)
|
195
|
+
|
196
|
+
'★出力ファイル名(納品日を追加)
|
197
|
+
strFile = ThisWorkbook.Path & "\" & wsClient.Cells(h, 1).Value & "_" & (Format(Date, "yyyymmdd")) & "_" & (Format(dayPickup, "yyyymmdd")) & "_" & (Format(p, "000"))
|
198
|
+
wsInvoice.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile & ".pdf" '選択したシートをPDF出力
|
199
|
+
|
200
|
+
'*PDF出力設定*
|
201
|
+
With wsInvoice.PageSetup
|
202
|
+
.Zoom = False '倍率をクリア
|
203
|
+
.FitToPagesWide = 1 '横方向に1ページに収める
|
204
|
+
.FitToPagesTall = 1 '縦方向に1ページに収める
|
205
|
+
.CenterHorizontally = True '水平方向に中央配置
|
206
|
+
.TopMargin = Application.CentimetersToPoints(1) '上マージンを1cm
|
207
|
+
.BottomMargin = Application.CentimetersToPoints(1) '下マージンを1cm
|
208
|
+
End With
|
209
|
+
|
210
|
+
wsInvoice.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile & ".pdf" '選択したシートをPDF出力
|
211
|
+
ActiveWorkbook.Close savechanges:=True, Filename:=strFile & ".xlsx" 'アクティブブックを名前を付けて保存して閉じる
|
212
|
+
|
213
|
+
p = p + 1
|
214
|
+
End If
|
215
|
+
Next dayLoop '★日付ループここまで
|
216
|
+
Next h
|
217
|
+
MsgBox "完了"
|
218
|
+
End Sub
|
219
|
+
```
|