Home » excel » excel – A pivot table with #N/A values?

excel – A pivot table with #N/A values?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am creating a macro which automates creating a pivot table. I am able to get an output for the pivot table, but all the data values are #N/A. This is due to the column that I utilize in the pivot table that contain values of #N/A.

How do I get the actual summation for all rows, without having to temper with the data set? This is the code that I am using so far.

Sub pivottable()

Dim PSheet As Worksheet, DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As pivottable
Dim PField As PivotField
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim PvtTable As pivottable
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True

Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets(1)
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(4, 1), _
TableName:="PivotTable4")

Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")

Sheets("PivotTable").Select
Set PvtTable = ActiveSheet.PivotTables("PivotTable")

'Rows
    With PvtTable.PivotFields("Office")
        .Orientation = xlRowField
        .Position = 1
    End With

    With PvtTable.PivotFields("Type")
        .Orientation = xlRowField
        .Position = 2
    End With
'Columns
    With PvtTable.PivotFields("Category")
        .Orientation = xlColumnField
        .Position = 1
    End With

PvtTable.AddDataField PvtTable.PivotFields("Receipts"), "receipts", xlSum

End Sub

I read that it might be possible to use AutoFilter . Would AutoFilter be able to ignore data with #N/A values or “0” and carry on summing other values, so we avoid having #N/A in our pivot table.

How to&Answers:

Wrap your VLOOKUP function in an IFERROR function, so that #N/A values are not returned in your source data.