选择数据透视表中不匹配的函数值

时间:2016-02-09 10:04:15

标签: excel excel-vba vba

我是VBA的新手,我正在编写此宏来从数据透视表中获取值并将它们存储在数据透视表附近的单元格中。 问题是当在数据透视表中找不到选择函数中写入的任何值时,它显示应用程序定义或对象定义的错误,我无法从选择中删除字段,因为在将来数据透视表中更新时可以存在该值。

在这方面,我们将不胜感激。

For i = 1 To 8

  Set rng = Intersect(pt.PivotFields("Category").PivotItems("Technical").DataRange.EntireRow, _
  pt.PivotFields("Office").PivotItems(Choose(i, "Mum", "blore", "chen", "delhi", "chandigarh", "hyd", "pune", "noida")) _
  .DataRange.EntireRow, pt.PivotFields("Total").DataRange)

  If rng Is Nothing Then

  Else

    Worksheets(6).Range(Choose(i, "AF29", "AF23", "AF24", "AF25", "AF30", "AF26", "AF27", "AF28")).value = CInt(WorksheetFunction.Percentile(rng, 0.95))

  End If

Next i

1 个答案:

答案 0 :(得分:0)

使用错误处理程序来处理该问题。

For i = 1 To 8
    On Error Resume Next
        Set Rng = Intersect(pt.PivotFields("Category").PivotItems("Technical").DataRange.EntireRow, _
        pt.PivotFields("Office").PivotItems(Choose(i, "Mum", "blore", "chen", "delhi", "chandigarh", "hyd", "pune", "noida")) _
        .DataRange.EntireRow, pt.PivotFields("Total").DataRange)
    On Error GoTo 0

    If Not Rng Is Nothing Then
        Worksheets(6).Range(Choose(i, "AF29", "AF23", "AF24", "AF25", "AF30", "AF26", "AF27", "AF28")).Value = CInt(WorksheetFunction.Percentile(Rng, 0.95))
    End If
Next i