My code (fragment):
[pre]
[/pre]
First run this macro I get error message "Error 1004 - unable to set NumberFormat property of PivotField object."
Running the same code second time with no error message.
The same procedure was used many times in different workbooks and different pivot tables and runs like a charm.
Any idea why this error happens and how to solve it?
[pre]
Code:
Sub CreatePivotZakazky()
Dim PTCache As PivotCache
Dim PT As PivotTable
Application.DisplayAlerts = False
If WorksheetExists("Zakázky") Then
Sheets("Zakázky").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End If
' Data source
Sheets("Dennik").Select ' already exists and filled with data
' Create the cache
Set PTCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion)
' Add a new sheet for the pivot table
Sheets.Add.Name = "Zakázky"
' Create the pivot table
Set PT = ActiveSheet.PivotTables.Add( _
PivotCache:=PTCache, _
TableDestination:=Range("A5"))
' Add the fields
With PT
.Name = "Naklady_Vynosy_Zakazky"
.PivotFields("Zakazka").Orientation = xlRowField
.PivotFields("Ucet").Orientation = xlRowField
With .PivotFields("Naklady")
.Orientation = xlDataField
.Caption = " Naklady"
.NumberFormat = "#,##0.00" ' the error occurs here
.Function = xlSum
End With
End With
End Sub
First run this macro I get error message "Error 1004 - unable to set NumberFormat property of PivotField object."
Running the same code second time with no error message.
The same procedure was used many times in different workbooks and different pivot tables and runs like a charm.
Any idea why this error happens and how to solve it?