• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Error 1004 Unable to set Numberformat property of PivotField

horvathg

New Member
My code (fragment):

[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
[/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?
 
Hi, horvathg!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the three first green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


Consider uploading a sample file (actually two, one that works fine and other where error arises), it'd be very useful for those who read this and might be able to help you. Thank you.

Give a look at the second green sticky post at this forums main page for uploading guidelines.


Regards!
 
Back
Top