-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcel4APivotFieldrInfo.cls
542 lines (425 loc) · 15.6 KB
/
Excel4APivotFieldrInfo.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "PivotFieldrInfo"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'VERSION 1.0 CLASS
Option Explicit
'Unrestricted class
Public Enum PivotTypes
Unknown = 0
PageType = 1
ColumnType = 2
RowType = 3
End Enum
Public Field As PivotField
Public Name As String
Private fieldItems As Collection
Public pivotType As PivotTypes
Public LabelFilter As PivotFilter
Public ValueFilter As PivotFilter
Private fieldBlankItem As PivotItemInfo
Private fieldAllItem As PivotItemInfo
Const FilterStrDelimator As String = ",. "
Public Property Get Items() As Collection
If fieldItems Is Nothing Then
Call Me.PopulateFieldItems
End If
Set Items = fieldItems
End Property
Public Property Get BlankItem() As PivotItemInfo
If fieldBlankItem Is Nothing Then
Dim pivotItem As pivotItem
Set pivotItem = Me.Field.PivotItems("(blank)")
Set fieldBlankItem = New PivotItemInfo
Call fieldBlankItem.SetPivotItem(pivotItem, Me)
End If
Set BlankItem = fieldBlankItem
End Property
Public Property Set BlankItem(item As PivotItemInfo)
Set fieldBlankItem = item
End Property
Public Property Get AllItem() As PivotItemInfo
If fieldAllItem Is Nothing Then
Set fieldAllItem = New PivotItemInfo
Call fieldAllItem.SetAsAllPivotItem(Me)
End If
Set AllItem = fieldAllItem
End Property
Public Property Set AllItem(item As PivotItemInfo)
Set fieldAllItem = item
End Property
Private Sub Class_Initialize()
Me.pivotType = Unknown
Me.Name = Empty
Set Me.LabelFilter = Nothing
Set Me.ValueFilter = Nothing
Set fieldItems = Nothing
Set fieldBlankItem = Nothing
Set fieldAllItem = Nothing
End Sub
Public Sub SetName(pField As PivotField)
On Error Resume Next
Dim sSetName As String
sSetName = Trim(pField.SourceName)
If IsEmpty(sSetName) = True Then
sSetName = Trim(pField.Name)
End If
Me.Name = sSetName
On Error GoTo 0
End Sub
Public Function GetItemName(pItem As pivotItem) As String
On Error Resume Next
GetItemName = Trim(pItem.SourceName)
If IsEmpty(GetItemName) = True Then
GetItemName = Trim(pItem.Caption)
End If
On Error GoTo 0
End Function
Public Function SetPivotField(pField As PivotField, pType As PivotTypes, Optional populateItems As Boolean = False) As PivotFieldrInfo
Set SetPivotField = Me
Set Me.Field = pField
Me.pivotType = pType
Call Me.SetName(pField)
If Not pField.pivotFilters Is Nothing And pField.pivotFilters.Count = 1 Then
If pField.pivotFilters(1).Active = True Then
If pField.pivotFilters(1).Order = -1 Then
Set Me.ValueFilter = pField.pivotFilters(1)
Else
Set Me.LabelFilter = pField.pivotFilters(1)
End If
End If
End If
If populateItems = True Then
Call Me.PopulateFieldItems
Else
Set fieldItems = Nothing
End If
End Function
Public Function PopulateFieldItems() As PivotFieldrInfo
Set PopulateFieldItems = Me
Dim pItem As pivotItem
Dim pItemInfo As PivotItemInfo
Set fieldItems = New Collection
For Each pItem In Me.Field.PivotItems
Set pItemInfo = New PivotItemInfo
Call pItemInfo.SetPivotItem(pItem, Me)
fieldItems.Add item:=pItemInfo, Key:=pItemInfo.Name
If pItemInfo.IsAll Then
Set Me.AllItem = pItemInfo
ElseIf pItemInfo.IsBlank Then
Set Me.BlankItem = pItemInfo
End If
Next pItem
End Function
Public Property Get MultiSelection() As Boolean
If Me.pivotType = PageType Then
MultiSelection = Me.Field.EnableMultiplePageItems = True
Else
MultiSelection = True
End If
End Property
Public Property Get Selected() As Collection
Dim pItem As PivotItemInfo
Set Selected = New Collection
If Me.MultiSelection = True Then
'Dim blankSelected As Boolean: blankSelected = False
For Each pItem In Me.Items
If pItem.Selected = True Then
Selected.Add item:=pItem, Key:=pItem.Name
'If pItem.IsBlank Then blankSelected = True
End If
Next pItem
If Me.Items.Count = Selected.Count Then 'IIf(blankSelected = True, Selected.Count, Selected.Count + 1)
Set Selected = Nothing
Set Selected = New Collection
Selected.Add item:=Me.AllItem, Key:=Me.AllItem.Name
End If
ElseIf Me.Field.CurrentPage = "(All)" Then
Selected.Add item:=Me.AllItem, Key:=Me.AllItem.Name
ElseIf Me.Field.CurrentPage = "(blank)" Then
Selected.Add item:=Me.BlankItem, Key:=Me.BlankItem.Name
Else
For Each pItem In Me.Items
If pItem.item.Caption = Me.Field.CurrentPage Then
Selected.Add item:=pItem, Key:=pItem.Name
Exit For
End If
Next pItem
End If
End Property
Public Property Get PivotTypeString() As String
Select Case Me.pivotType
Case PageType
PivotTypeString = "Page"
Case ColumnType
PivotTypeString = "Column"
Case RowType
PivotTypeString = "Row"
Case Else
PivotTypeString = Empty
End Select
End Property
Public Property Let PivotTypeString(strType As String)
Select Case strType
Case "Page"
Me.pivotType = PageType
Case "Column"
Me.pivotType = ColumnType
Case "Row"
Me.pivotType = RowType
Case Else
Me.pivotType = Unknown
End Select
End Property
Public Property Get IsExpanded() As Boolean
On Error GoTo Done
IsExpanded = False
If Not Me.pivotType = RowType Then Exit Property
If IsNumeric(Me.Field.Position) Then
If Me.Field.ShowDetail = True Then
IsExpanded = True
End If
End If
Done:
On Error GoTo 0
End Property
Public Property Get CanExpand() As Boolean
On Error GoTo Done
CanExpand = False
If Not Me.pivotType = RowType Then Exit Property
If IsNumeric(Me.Field.Position) Then
If Me.Field.ShowDetail = True Then
CanExpand = True
Else
CanExpand = True
End If
End If
Done:
On Error GoTo 0
End Property
Public Function Expand(expandField As Boolean) As PivotFieldrInfo
On Error GoTo Done
Set Expand = Me
If Not Me.pivotType = RowType Then Exit Function
If IsNumeric(Me.Field.Position) Then
'If Not Me.Field.ShowDetail = expandField Then
Me.Field.ShowDetail = expandField
'End If
End If
Done:
On Error GoTo 0
End Function
Public Property Get SelectedValues() As Collection
Dim pItem As PivotItemInfo
Set SelectedValues = New Collection
If Me.MultiSelection = True Then
'Dim blankSelected As Boolean: blankSelected = False
For Each pItem In Me.Items
If pItem.Selected = True Then
SelectedValues.Add pItem.Name
'If pItem.IsBlank Then blankSelected = True
End If
Next pItem
If Me.Items.Count = Selected.Count Then 'IIf(blankSelected = True, Selected.Count, Selected.Count + 1)
Set SelectedValues = Nothing
Set SelectedValues = New Collection
SelectedValues.Add Me.AllItem.Name
End If
ElseIf Me.Field.CurrentPage = "(All)" Then
SelectedValues.Add Me.AllItem.Name
ElseIf Me.Field.CurrentPage = "(blank)" Then
SelectedValues.Add Me.BlankItem.Name
Else
For Each pItem In Me.Items
If pItem.item.Caption = Me.Field.CurrentPage Then
SelectedValues.Add pItem.Name
Exit For
End If
Next pItem
End If
End Property
Public Property Get value() As String
Dim firstItem As Boolean: firstItem = True
Dim strValue As Variant
value = Empty
For Each strValue In Me.SelectedValues
If firstItem = True Then
value = strValue
firstItem = False
Else
value = value + FilterStrDelimator + strValue
End If
Next strValue
End Property
Public Property Let value(filterStr As String)
Call Me.MatchAndSelectString(filterStr, False, True)
End Property
Public Function DeSelectAll() As PivotFieldrInfo
Set DeSelectAll = Me
If Me.MultiSelection = True Then
Dim pfItem As PivotItemInfo
Me.BlankItem.item.Visible = True
For Each pfItem In Me.Items
If pfItem.IsBlank = False Then
pfItem.Selected = False
End If
Next pfItem
Else
Me.BlankItem.Selected = True
End If
End Function
Public Function SelectBlank() As PivotFieldrInfo
Set SelectBlank = Me.DeSelectAll
End Function
Public Function SelectAll() As PivotFieldrInfo
Set SelectAll = Me
Me.Field.ClearManualFilter
End Function
Public Function Matched(matchValues() As String, patternMatch As Boolean) As Collection
Dim value As String
Dim valueLBnd As Long: valueLBnd = LBound(matchValues)
Dim valueUBnd As Long: valueUBnd = UBound(matchValues)
Dim valueCnt As Long: valueCnt = valueUBnd - valueLBnd + 1
Dim valueIdx As Long
Dim pItemInfo As PivotItemInfo
Set Matched = New Collection
If valueCnt = 0 Then Exit Function
If valueCnt = 1 And matchValues(valueLBnd) = "(All)" Then
Matched.Add Me.AllItem
ElseIf fieldItems Is Nothing Then
Dim pItem As pivotItem
If patternMatch = True Then
For Each pItem In Me.Field.PivotItems
For valueIdx = valueLBnd To valueUBnd
If GetItemName(pItem) Like matchValues(valueIdx) Then
Set pItemInfo = New PivotItemInfo
Call pItemInfo.SetPivotItem(pItem, Me)
Matched.Add pItemInfo
Exit For
End If
Next valueIdx
Next pItem
Else
On Error Resume Next
For valueIdx = valueLBnd To valueUBnd
Set pItem = Nothing
Set pItem = Me.Field.PivotItems(matchValues(valueIdx))
If Not pItem Is Nothing Then
Set pItemInfo = New PivotItemInfo
Call pItemInfo.SetPivotItem(pItem, Me)
Matched.Add pItemInfo
If Matched.Count = valueCnt Then Exit Function
End If
Next valueIdx
On Error GoTo 0
End If
Else
For Each pItemInfo In Me.Items
For valueIdx = valueLBnd To valueUBnd
If IIf(patternMatch = True, pItemInfo.Name Like matchValues(valueIdx), matchValues(valueIdx) = pItemInfo.Name) Then
Matched.Add pItemInfo
If patternMatch = False And Matched.Count = valueCnt Then Exit Function
Exit For
End If
Next valueIdx
Next pItemInfo
End If
End Function
Public Function MatchAndSelect(matchValues() As String, patternMatch As Boolean, resetFilter As Boolean) As Collection
Dim value As String
Dim pItemInfo As PivotItemInfo
Dim valueLBnd As Long: valueLBnd = LBound(matchValues)
Dim valueUBnd As Long: valueUBnd = UBound(matchValues)
Dim valueCnt As Long: valueCnt = valueUBnd - valueLBnd + 1
Dim valueIdx As Long
Set MatchAndSelect = New Collection
If valueCnt = 0 Then Exit Function
If valueCnt = 1 Then
If matchValues(valueLBnd) = "(All)" Then
Call Me.SelectAll
MatchAndSelect.Add Me.AllItem
Exit Function
ElseIf matchValues(valueLBnd) = "(blank)" Then
Call Me.SelectBlank
MatchAndSelect.Add Me.BlankItem
Exit Function
End If
End If
Dim blankMatched As Boolean
Dim localMS As Boolean: localMS = Me.MultiSelection
If localMS = True Then
If Me.BlankItem.item.Visible = False Then
Me.BlankItem.item.Visible = True
ElseIf resetFilter = False Then
blankMatched = True
End If
Else
For valueIdx = valueLBnd To valueUBnd
If IIf(patternMatch = True, Me.Field.CurrentPage Like matchValues(valueIdx), matchValues(valueIdx) = Me.Field.CurrentPage) Then
Set MatchAndSelect = Me.Selected
Exit Function
End If
Next valueIdx
End If
Dim vSelected As Boolean
Dim checkValues As Boolean: checkValues = True
If fieldItems Is Nothing Then
Dim pItem As pivotItem
Dim pItemName As String
For Each pItem In Me.Field.PivotItems
vSelected = False
pItemName = Me.GetItemName(pItem)
If checkValues = True Then
For valueIdx = valueLBnd To valueUBnd
If IIf(patternMatch = True, pItemName Like matchValues(valueIdx), matchValues(valueIdx) = pItemName) Then
Set pItemInfo = New PivotItemInfo
Call pItemInfo.SetPivotItem(pItem, Me)
MatchAndSelect.Add pItemInfo
pItemInfo.Selected = True
If localMS = False Then Exit Function
vSelected = True
If pItemInfo.Name = "(blank)" Then blankMatched = True
checkValues = patternMatch = True Or MatchAndSelect.Count < valueCnt
Exit For
End If
Next valueIdx
End If
If localMS = True And resetFilter = True And vSelected = False And Not pItemName = "(blank)" Then
pItem.Visible = False
End If
Next pItem
Else
For Each pItemInfo In Me.Items
vSelected = False
If checkValues = True Then
For valueIdx = valueLBnd To valueUBnd
If IIf(patternMatch = True, pItemInfo.Name Like matchValues(valueIdx), matchValues(valueIdx) = pItemInfo.Name) Then
MatchAndSelect.Add pItemInfo
pItemInfo.Selected = True
If localMS = False Then Exit Function
vSelected = True
If pItemInfo.Name = "(blank)" Then blankMatched = True
checkValues = patternMatch = True Or MatchAndSelect.Count < valueCnt
Exit For
End If
Next valueIdx
End If
If localMS = True And resetFilter = True And vSelected = False And Not pItemInfo.Name = "(blank)" Then
pItemInfo.Selected = False
End If
Next pItemInfo
End If
If MatchAndSelect.Count = 0 Then
If resetFilter = True Then Call Me.SelectBlank
ElseIf localMS = True And blankMatched = False Then Me.BlankItem.item.Visible = False
End If
End Function
Public Function MatchAndSelectString(matchValueStr As String, patternMatch As Boolean, resetFilter As Boolean) As Collection
Dim matchValues() As String: matchValues = Split(matchValueStr, FilterStrDelimator)
Set MatchAndSelectString = Me.MatchAndSelect(matchValues, patternMatch, resetFilter)
End Function