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
'Title:
'Setting the number of Decimal digits of each percentages in a Pivot Table

'Description:
'Q. 
'I'm using SPSS 7.5 or higher. 
'I would like to set the number of Decimal digits displayed in a Pivot Table.
'Is there a way to automate this?

'A. 
'Below is a script which will make the changes for the currently selected Pivot Table.
'Sub Main may be modified or discarded; however, the SetDecimalDigits subroutines,
'Function will probably not need modification should you wish to use them in your own
'scripts, or Call them from an Autoscript routine.
'This script is a modification of AnswerNet Solution ID:  100007757
'Posted to SPSSX-L list by Raynald Levesque on 2003/03/11

'Begin Description
'SetDecimalDigits sets cell formats of all percentages to the specified number of digits.
'End Description

Sub Main()
Dim objPivot As PivotTable 
Dim objItem As ISpssItem 
Dim bolFoundOutputDoc As Boolean
Dim bolPivotSelected As Boolean
'Pivot table must be activated

Call GetFirstSelectedPivot(objPivot, objItem, bolFoundOutputDoc, bolPivotSelected)

If (bolFoundOutputDoc = False) Or (bolPivotSelected = False) Then
'either there wasn't an output doc or a pivot table wasn't selected
Exit Sub
End If

'postpone drawing until we're finished
objPivot.UpdateScreen = False 

'set all cell formats to 2 decimal digits
Call SetDecimalDigits(objPivot, 2)

objPivot.UpdateScreen = True
objItem.Deactivate
'sometimes, the PivotTable Height and Width are not updated correctly 
're-activating the table will work around this 
objItem.ActivateTable 
objItem.Deactivate 
End Sub 


Sub SetDecimalDigits(objPivot As PivotTable, intDigits As Integer) 
'Changes the number of Decimal Digits for *all* cells containing "%"
Dim lngRow As Long, lngCol As Long
Dim objDataCells As ISpssDataCells 

Set objDataCells = objPivot.DataCellArray 
	With objDataCells
		For lngRow = 0 To .NumRows - 1
			For lngCol = 0 To .NumColumns - 1
			If Not IsNull (.ValueAt (lngRow, lngCol)) And InStr(.NumericFormatAt(lngRow,lngCol),"%")>0 Then
			.HDecDigitsAt (lngRow, lngCol) = intDigits
			End If
			Next
		Next
	End With
objPivot.Autofit
End Sub