Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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 检查PT表格数据字段是否存在VBA_Excel_Vba_Pivot Table - Fatal编程技术网

Excel 检查PT表格数据字段是否存在VBA

Excel 检查PT表格数据字段是否存在VBA,excel,vba,pivot-table,Excel,Vba,Pivot Table,我的代码运行完美。。。但不知何故,它每次运行时都会添加一个数据字段。我试着用测试它是否存在,如果pf.Orientation=xlDataField=True,那么…(“不要添加xlDataField”)…仍然没有结果 我很确定我遗漏了一些重要的东西,否则代码运行良好。如果已经存在另一个字段,是否有方法不添加该字段 Sub Import() Dim ws As Worksheet, pvtCache As PivotCache Dim wsp As Worksheet

我的代码运行完美。。。但不知何故,它每次运行时都会添加一个数据字段。我试着用
测试它是否存在,如果pf.Orientation=xlDataField=True,那么…(“不要添加xlDataField”)…
仍然没有结果

我很确定我遗漏了一些重要的东西,否则代码运行良好。如果已经存在另一个字段,是否有方法不添加该字段

Sub Import()

    Dim ws As Worksheet, pvtCache As PivotCache
    Dim wsp As Worksheet
    Dim pf As PivotField, pt As PivotTable

    Set wsp = Sheets("Pivot")
    Set pt = wsp.PivotTables("PivotTable")

    ActiveWorkbook.RefreshAll

    Sheets("TP_Coois").Cells.Replace ",", "", xlPart
    Set pf = pt.PivotFields("Sales")

    If pf.Orientation = xlDataField = True Then  '***I believe here is something wrong***
        pt.PivotCache.Refresh
    Else
        With pf
            .Orientation = xlDataField
            .Function = xlSum
        End With

        pt.PivotCache.Refresh
    End If

End Sub

感谢您的帮助

尝试将您的条件更改为:

If pf.Orientation = xlDataField = True Then
为此:

If pf.Orientation = xlDataField Then

有没有可能工作表(“TP_Coois”).Cells.Replace“,”和“,”xlPart正在添加单元格?谢谢,伙计,我尝试过,但没有=True,但仍然添加了相同的字段。嗨,布莱恩,你猜怎么着,我们很接近了,但解决这个问题的最佳方法是“If pf.Name=“Sales”Then…”所有这些都归功于vbaexpress.com中的Rory