Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 选择透视表中不匹配的函数值_Excel_Vba - Fatal编程技术网

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

Excel 选择透视表中不匹配的函数值,excel,vba,Excel,Vba,我是VBA新手,我编写这个宏是为了从数据透视表中获取值,并将它们存储在数据透视表附近的单元格中。 问题是,当choose函数中写入的任何值在透视表中找不到时,它显示的是应用程序定义的或对象定义的错误,我无法从choose中删除字段,因为该值可能在将来透视表更新时出现 在这方面,任何帮助都将不胜感激 For i = 1 To 8 Set rng = Intersect(pt.PivotFields("Category").PivotItems("Technical").DataRange.E

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

在这方面,任何帮助都将不胜感激

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

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

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

实际上Choose()不会在代码中引起任何错误。因为循环将从1变为8,Choose()将检索关联循环计数器的相关单元格引用。但在某些情况下,数据透视表中不存在该值,如何在找不到该值时跳过循环。或者有其他方法吗?