povit_add
Function povit_add1(field_reslt, sh_to, Optional field_r = "", _
Optional field_c = "", Optional sh_from = "", Optional rng_from = "a1", _
Optional pname = "tmp1", Optional psn = "r1c1", Optional tab_reslt = 1)
'---
If sh_from = "" Then sh_from = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets(sh_from).Range(rng_from).CurrentRegion, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=sh_to & "!" & psn, TableName:=pname, DefaultVersion:= _
xlPivotTableVersion12
'---先添加result,若后添加,会覆盖field_r的相同的项
If tab_reslt = 1 Then
Sheets(sh_to).PivotTables(pname).AddDataField Sheets(sh_to).PivotTables(pname _
).PivotFields(field_reslt), "reslt", xlSum
Else
Sheets(sh_to).PivotTables(pname).AddDataField Sheets(sh_to).PivotTables(pname _
).PivotFields(field_reslt), "reslt", xlCount
End If
'---
If field_r <> "" Then
With Sheets(sh_to).PivotTables(pname).PivotFields(field_r)
.Orientation = xlRowField
.Position = 1
End With
End If
If field_c <> "" Then
With Sheets(sh_to).PivotTables(pname).PivotFields(field_c)
.Orientation = xlColumnField
.Position = 1
End With
End If
'---sort
'On Error Resume Next
'Sheets(sh_to).PivotTables(pname).DataRange.Sort Order1:=xlAscending, Type:=xlSortLabels, _
'OrderCustom:=13, Orientation:=xlTopToBottom, SortMethod:=xlStroke
'On Error Resume Next
'Sheets(sh_to).PivotTables(pname).DataRange.Sort Order1:=xlAscending, Type:=xlSortLabels, _
'OrderCustom:=14, Orientation:=xlLeftToRight, SortMethod:=xlStroke
End Function
Function pivot_item_hide(sh_to, tname, arr12, field1)
' arr12 = get_data_arr("data_12")
With Sheets(sh_to).PivotTables(tname).PivotFields(field1)
For Each k1 In arr12
.PivotItems(k1).Visible = False
Next
End With
End Function