【问题标题】:Type mistmatch error - Pivot table filtering through Excel VBA类型不匹配错误 - 通过 Excel VBA 过滤数据透视表
【发布时间】:2011-10-08 01:02:21
【问题描述】:

我一直在尝试让枢轴过滤器使用输入框工作。我从http://www.ozgrid.com/VBA/hide-pivot-fields.htm 窃取了大部分代码,并一直应用到我的电子表格中,并不断收到我无法弄清楚的类型不匹配错误。该示例是为数字构建的,我需要对文本进行过滤。

错误查找器在 Evaluate 函数上停止,但两个变量都定义为文本,所以我认为它一定是 Org 变量..

Sub HideByCriteria()
Dim pt As PivotTable, pi As PivotItem
Dim Org As String
Dim strCri As String
Dim bHide As Boolean
Dim xlCalc As XlCalculation

    Set pt = Sheets("Req Posting Status").PivotTables("PivotTable1")
    
        strCri = InputBox("Enter Organization" _
            & Chr(13) & "Valid Criteria Examples:" _
            & Chr(13) & "HCS-CT for all HCS-CT orgs" _
            & Chr(13) & "or CKS for any org that contains CKS", "HIDE AGE")

     If strCri = vbNullString Then Exit Sub
     strCri = Trim(strCri)
     
     pt.ManualUpdate = True
     With Application
            xlCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
     End With

    'On Error GoTo NonValidCriteria:
    
        For Each pi In pt.PivotFields("Organization").PivotItems
            Org = pi
            bHide = Evaluate(Org & strCri)
            pi.Visible = bHide
        Next pi
     
    pt.ManualUpdate = False
    With Application
            .Calculation = xlCalc
            .ScreenUpdating = True
     End With
    
Exit Sub

【问题讨论】:

    标签: excel filtering pivot-table type-mismatch vba


    【解决方案1】:

    下面的代码在 Excel 2003 中进行了测试,运行良好。如果您有任何其他问题,请告诉我。上面贴的有很多冗余代码。

    KISS - 保持简单愚蠢。

    Public Sub HideMacro()
    Dim pi As PivotItem
    Dim critera As String
    
    critera = LCase$(Trim$(InputBox("Do you want to see HCS-CT or CKS?", "Critera", "CKS")))
    
    If critera = vbNullString Then
        MsgBox "Cancelled Filter", vbInformation + vbOKOnly, "Information"
        Exit Sub
    ElseIf critera <> "hcs-ct" And critera <> "cks" Then
        MsgBox "Invalid entry. Please try again.", vbCritical + vbOKOnly, "Invalid Entry"
        Exit Sub
    End If
    
    With Sheet4.PivotTables(1)
        .ManualUpdate = True
        For Each pi In .PivotFields("Orginization").PivotItems
            pi.Visible = (LCase$(pi.Value) = critera) 'Hides other value and shows wanted value.
        Next
        .ManualUpdate = False
    End With
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 2015-08-27
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多