回答編集履歴
3
使わないかもだけど4/2追加分一部修正しました
answer
CHANGED
@@ -200,13 +200,14 @@
|
|
200
200
|
Dim r2 As Range
|
201
201
|
Dim y
|
202
202
|
ws.Activate
|
203
|
+
Set rr = d.Areas(1).Offset(, -1)
|
203
204
|
For Each r1 In d.Areas
|
204
205
|
r1.Value = r1.Value
|
205
206
|
pt.PivotSelect r1(1).Offset(-2).Value, xlLabelOnly
|
206
207
|
For Each r2 In Selection.Offset(, 1)
|
207
208
|
y = Application.Match(CLng(r2.Value), rr, 0)
|
208
209
|
If IsNumeric(y) Then
|
209
|
-
r1(y).Value = r1(y).Value & "|" & r2.Offset(, 1).Value
|
210
|
+
r1(y).Value = CStr(r1(y).Value) & "|" & r2.Offset(, 1).Value
|
210
211
|
End If
|
211
212
|
Next
|
212
213
|
Next
|
2
サンプル追加
answer
CHANGED
@@ -94,4 +94,127 @@
|
|
94
94
|
' r.Value = r.Value
|
95
95
|
'Next
|
96
96
|
End Sub
|
97
|
+
```
|
98
|
+
(2020.04.02追加)
|
99
|
+
..なんか自分でも納得いかないコードですが..
|
100
|
+
すみません、「こんなアプローチもあります」的なサンプル扱いでお願いします
|
101
|
+
|
102
|
+
```VBA
|
103
|
+
Sub test2()
|
104
|
+
Dim r As Range
|
105
|
+
Dim d As Range
|
106
|
+
Dim dd As Range
|
107
|
+
Dim cc As Range
|
108
|
+
Dim rr As Range
|
109
|
+
Dim x As Long
|
110
|
+
Dim i As Long
|
111
|
+
Dim j As Long
|
112
|
+
Dim k As Long
|
113
|
+
|
114
|
+
'予定表の範囲を取得
|
115
|
+
With Sheets("予定表")
|
116
|
+
Set r = .Range("A2").CurrentRegion
|
117
|
+
Set dd = .Range("B3", r(r.Count))
|
118
|
+
End With
|
119
|
+
Set cc = dd.Resize(1).Offset(-1)
|
120
|
+
Set rr = dd.Resize(, 1).Offset(, -1)
|
121
|
+
|
122
|
+
Dim pt As PivotTable
|
123
|
+
Dim st As String
|
124
|
+
Dim ws As Worksheet
|
125
|
+
Set ws = Sheets.Add
|
126
|
+
|
127
|
+
With Sheets("日報")
|
128
|
+
x = .Cells(.Rows.Count, 10).End(xlUp).Row
|
129
|
+
k = x + 1 '最後のデータクリアで使う
|
130
|
+
|
131
|
+
'日報から氏名、日付、略号場所キーでピボット作成 _
|
132
|
+
後で日報ベースの予定|実績の実績データ追加で使う
|
133
|
+
Set r = .Range("A1", .Cells(x, "L"))
|
134
|
+
r.Columns("L").Formula = "=B1&H1"
|
135
|
+
st = .Range("L1").Value
|
136
|
+
Set pt = ActiveWorkbook.PivotCaches.Add( _
|
137
|
+
SourceType:=xlDatabase, _
|
138
|
+
SourceData:=r) _
|
139
|
+
.CreatePivotTable(ws.Range("A1"))
|
140
|
+
pt.ColumnGrand = False
|
141
|
+
pt.RowGrand = False
|
142
|
+
pt.RowAxisLayout xlTabularRow
|
143
|
+
With pt.PivotFields("氏名")
|
144
|
+
.Orientation = xlRowField
|
145
|
+
.Subtotals(1) = False
|
146
|
+
End With
|
147
|
+
With pt.PivotFields("日付")
|
148
|
+
.Orientation = xlRowField
|
149
|
+
.Subtotals(1) = False
|
150
|
+
End With
|
151
|
+
pt.PivotFields(st).Orientation = xlRowField
|
152
|
+
pt.AddDataField pt.PivotFields("執務時間"), "執務時間計", xlSum
|
153
|
+
'予定表の氏名と日付を日報に追加
|
154
|
+
x = x + 1
|
155
|
+
For i = 1 To rr.Count
|
156
|
+
For j = 1 To cc.Count
|
157
|
+
If dd(i, j).Value <> "" Then
|
158
|
+
.Cells(x, "D").Value = rr(i).Value
|
159
|
+
.Cells(x, "J").Value = cc(j).Value
|
160
|
+
x = x + 1
|
161
|
+
End If
|
162
|
+
Next
|
163
|
+
Next
|
164
|
+
Set r = .Range("A1", .Cells(x - 1, "L"))
|
165
|
+
End With
|
166
|
+
'ピボット利用して「結合」シート作成
|
167
|
+
Dim ws2 As Worksheet
|
168
|
+
Set ws2 = Sheets.Add
|
169
|
+
With ActiveWorkbook.PivotCaches.Add( _
|
170
|
+
SourceType:=xlDatabase, _
|
171
|
+
SourceData:=r) _
|
172
|
+
.CreatePivotTable(ws2.Range("A1"))
|
173
|
+
.ColumnGrand = False
|
174
|
+
.RowGrand = False
|
175
|
+
.RowAxisLayout xlTabularRow
|
176
|
+
.PivotFields("日付").Orientation = xlRowField
|
177
|
+
.PivotFields("氏名").Orientation = xlColumnField
|
178
|
+
.AddDataField .PivotFields("略号"), "予定|実績", xlCount
|
179
|
+
.AddDataField .PivotFields("執務時間"), "執務時間計", xlSum
|
180
|
+
.DataPivotField.Orientation = xlColumnField
|
181
|
+
'"予定|実績"の範囲取得
|
182
|
+
.PivotSelect "予定|実績", xlDataOnly
|
183
|
+
Set d = Selection
|
184
|
+
'PivotTableを解除
|
185
|
+
.TableRange2.Copy
|
186
|
+
.TableRange2.PasteSpecial xlPasteValues
|
187
|
+
End With
|
188
|
+
Application.CutCopyMode = False
|
189
|
+
|
190
|
+
'予定表から予定|実績データ追加 _
|
191
|
+
数式文字列設定 =INDEX(dd,MATCH($A4,rr,0),MATCH(B$2,cc,0))&""
|
192
|
+
Dim s(2) As String
|
193
|
+
s(0) = "=index(" & dd.Address(, , , True)
|
194
|
+
s(1) = "match($A4," & rr.Address(, , , True) & ",0)"
|
195
|
+
s(2) = "match(B$2," & cc.Address(, , , True) & ",0))&"""""
|
196
|
+
d.Formula = Join(s, ",")
|
197
|
+
|
198
|
+
'日報ベースの予定|実績の実績データ追加
|
199
|
+
Dim r1 As Range
|
200
|
+
Dim r2 As Range
|
201
|
+
Dim y
|
202
|
+
ws.Activate
|
203
|
+
For Each r1 In d.Areas
|
204
|
+
r1.Value = r1.Value
|
205
|
+
pt.PivotSelect r1(1).Offset(-2).Value, xlLabelOnly
|
206
|
+
For Each r2 In Selection.Offset(, 1)
|
207
|
+
y = Application.Match(CLng(r2.Value), rr, 0)
|
208
|
+
If IsNumeric(y) Then
|
209
|
+
r1(y).Value = r1(y).Value & "|" & r2.Offset(, 1).Value
|
210
|
+
End If
|
211
|
+
Next
|
212
|
+
Next
|
213
|
+
'追加したダミーデータと作業用Sheetをクリア
|
214
|
+
r.Columns("L").ClearContents
|
215
|
+
Range(r(k, 1), r(r.Count)).ClearContents
|
216
|
+
Application.DisplayAlerts = False
|
217
|
+
ws.Delete
|
218
|
+
Application.DisplayAlerts = True
|
219
|
+
End Sub
|
97
220
|
```
|
1
別案
answer
CHANGED
@@ -35,4 +35,63 @@
|
|
35
35
|
氏名は行固定で B$2
|
36
36
|
|
37
37
|
修正後の式を下方向と各氏名の後に挿入した列にコピーすると良いです
|
38
|
-
必要に応じて要所要所をマクロ化すれば良いでしょう
|
38
|
+
必要に応じて要所要所をマクロ化すれば良いでしょう
|
39
|
+
|
40
|
+
(追記)
|
41
|
+
ぁ一応、逆に日報ベースの別案も作ってはいましたけどね
|
42
|
+
ピボットをベースに予定表の内容がMatchするものをひっぱってくる感じ。
|
43
|
+
提示のレイアウト限定ではありますが
|
44
|
+
```VBA
|
45
|
+
Sub test()
|
46
|
+
Dim r As Range
|
47
|
+
Dim d As Range
|
48
|
+
Dim dd As Range
|
49
|
+
Dim cc As Range
|
50
|
+
Dim rr As Range
|
51
|
+
|
52
|
+
With Sheets("日報")
|
53
|
+
'PivotTableのSourceData
|
54
|
+
Set r = .Range("K1", .Cells(.Rows.Count, 4).End(xlUp).Offset(, -3))
|
55
|
+
End With
|
56
|
+
|
57
|
+
'とりあえず新規Sheetに集計してみる
|
58
|
+
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
|
59
|
+
SourceData:=r).CreatePivotTable("")
|
60
|
+
.PivotFields("日付").Orientation = xlRowField
|
61
|
+
.PivotFields("氏名").Orientation = xlColumnField
|
62
|
+
'"略号"の集計はダミー。後で数式セット
|
63
|
+
.AddDataField .PivotFields("略号"), "略号&場所", xlCount
|
64
|
+
.AddDataField .PivotFields("執務時間"), "執務時間計", xlSum
|
65
|
+
.DataPivotField.Orientation = xlColumnField
|
66
|
+
.ColumnGrand = False
|
67
|
+
.RowGrand = False
|
68
|
+
.RowAxisLayout xlTabularRow
|
69
|
+
'"略号&場所"の範囲取得
|
70
|
+
.PivotSelect "'略号&場所'", xlDataOnly
|
71
|
+
Set d = Selection
|
72
|
+
.TableRange2.Copy
|
73
|
+
'PivotTableを解除
|
74
|
+
.TableRange2.PasteSpecial xlPasteValues
|
75
|
+
End With
|
76
|
+
Application.CutCopyMode = False
|
77
|
+
|
78
|
+
'数式の参照先アドレスget
|
79
|
+
With Sheets("予定表")
|
80
|
+
Set r = .Range("A2").CurrentRegion
|
81
|
+
Set dd = .Range("B3", r(r.Count))
|
82
|
+
End With
|
83
|
+
Set cc = dd.Resize(1).Offset(-1)
|
84
|
+
Set rr = dd.Resize(, 1).Offset(, -1)
|
85
|
+
|
86
|
+
'数式文字列設定 =INDEX(dd,MATCH($A4,rr,0),MATCH(B$2,cc,0))&""
|
87
|
+
Dim s(2) As String
|
88
|
+
s(0) = "=index(" & dd.Address(, , , True)
|
89
|
+
s(1) = "match($A4," & rr.Address(, , , True) & ",0)"
|
90
|
+
s(2) = "match(B$2," & cc.Address(, , , True) & ",0))&"""""
|
91
|
+
d.Formula = Join(s, ",")
|
92
|
+
'数式の値化が必要であれば以下コメント活かす
|
93
|
+
'For Each r In d.Areas
|
94
|
+
' r.Value = r.Value
|
95
|
+
'Next
|
96
|
+
End Sub
|
97
|
+
```
|