【问题标题】:Iserror not working错误不工作
【发布时间】:2014-05-20 17:17:35
【问题描述】:

我的代码有问题。 我正在尝试激活一个代码,该代码在一个工作表中获取一个单元格并过滤另一个数据透视表中的数据,以防该值不存在,有一个 msgbox 显示存在错误。 我的问题是,当该值为 true 时,我希望它显示 msgbox“该值不存在于数据透视表中”。当“if”为假时,我需要过滤数据,但它不起作用。 代码如下:

Sub MM()
    Sheets("sheets1").Select
    Selection.Copy
    Sheets("pivot").Select
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
    ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
    If Not IsError(ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value) Then
        MsgBox ("the value dosen't exists in the pivot")
         Sheets("sheets1").Select
    Else
        ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
    End If
End Sub

我会很高兴得到一些帮助!

【问题讨论】:

    标签: excel filtering pivot-table raiserror vba


    【解决方案1】:

    不完全确定是否要根据所选单元格中的内容过滤枢轴,但这是我的建议。要指出有一种方法可以过滤具有许多值的枢轴,但我想您希望过滤器只针对一个值完成?此外,将过滤器添加到枢轴的方法是循环遍历所有字段值并将它们设置为可见或不可见。

    Sub testi2()
        'Bit waisty way to do it, you could just make a variable to hold the value -
        Dim myValue As Variant
        myValue = ActiveCell.Value
        'Sheets("sheets1").Select
        'Selection.Copy
        Sheets("pivot").Select
        'Range("C1").Select
        'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        'Your choise tough, if you really need to copy the value to the cell C1 then by all 
        'means do, but you should still send the value to variable for code will be easier 
        'to be handled and clearer to read.
        'Here you could also clear all past filters for the pivot if needed. 
        'I won't encourage to but if there are other filters present exept
        'what is in filterWBS field, the code will run into an error. 
    
        Dim pItem As PivotItem
        Dim ifFound As Boolean
        ifFound = False
    
        'loop trough the pivotfieldvalues to see if match exists, pivot tables have a need for at least one visible value.
        For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems
    
            'if data exists then ifFound value will be set to true
            If pItem = myValue Then
                ifFound = True
            End If
        Next
    
        'based on the if value found set fields visible or hidden
        If ifFound = True Then
            For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems
                If pItem <> myValue Then
                    pItem.Visible = False
                Else
                    pItem.Visible = True
                End If
            Next
        'if the value was not present show the message box
        Else
            MsgBox ("the value doesn't exists in the pivot")
            'You could in this case clear the filter
        End If
    End Sub
    

    【讨论】:

    • 感谢您的帮助!当值在数据透视表中时,我仍然遇到问题,代码向我显示 MSGBOX 而不是数据透视表中的数据...
    • 没问题,你检查过数据和输入值是同一类型吗?对于数字,值可以作为字符串或数值传递。如果是这样,您可以尝试将输入数字写入单元格为 '5 并带有星号,以检查是否是您的问题。此外,如果数据不在“过滤 WBS”字段中,则不会计算,但在这种情况下,您应该参考具有该值的字段。
    • 还有一个问题。当我检查代码时,我发现第一个循环是无穷无尽的......
    【解决方案2】:

    我找到了解决问题的方法。

    Sub MM()
    Sheets("Sheets1").Select
    Selection.Copy
    Sheets("Pivot").Select
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
    ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
    On Error GoTo msg
    ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
    Exit Sub
    
    msg:
    MsgBox ("There is no data for this WBS in pivot")
    Sheets("sheets1").Select
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2016-09-04
      • 2016-05-21
      • 2015-10-08
      相关资源
      最近更新 更多