热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用VBA-通配符过滤器更新和过滤枢轴-UpdateandfilterpivotwithVBA-Wildcardfilter

IwanttocleartheprevoiusfilteronpivotfieldInvoicenr,updateapivottable,andnotshowcert

I want to clear the prevoius filter on pivotfield Invoicenr, update a pivot table, and not show certain items.
I want to show Everything but the items that have a Invoicenr that begins with PO* (seems that * can't be used in VBA?).
Besides this I want to see everything else and the Invoicenr that starts with PO and contains OH.

我希望清除数据透视字段Invoicenr上的prevoius过滤器,更新一个pivot表,并且不显示某些项。我想要展示所有的东西,但是有一个从PO*开始的Invoicenr的项目(似乎在VBA中不能使用*)。除此之外,我还想看看其他的东西和以PO开头并包含OH的Invoicenr。

See my attempt below:

看到我尝试如下:

Sub Macro2()
'
' Macro2 Macro
'
    ThisWorkbook.RefreshAll    
      'Worksheets("Pivot").Select
      'ActiveSheet.PivotTables("PIVOT1").RepeatAllLabels xlRepeatLabels
    ActiveSheet.PivotTables("PIVOT1").PivotFields("Invoicenr"). _
        ClearLabelFilters              
    With ActiveSheet.PivotTables("PIVOT1").PivotFields("invoicenr")
        .PivotItems("PO").Visible = False         
    End With        
End Sub

2 个解决方案

#1


1  

Use this code:

使用这段代码:

   Sub Except_PO()

      Dim var As Variant

      var = "PO*"

     ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").ClearAllFilters
    ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").PivotFilters. _
    Add Type:=xlCaptionDoesNotEqual, Value1:=var

   End Sub


  Sub POwithOH()

   Dim var As Variant

    var = "PO*OH*"

    ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").ClearAllFilters
    ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").PivotFilters. _
      Add Type:=xlCaptionEquals, Value1:=var


  End Sub

Then make 2 command buttons with this code

然后用此代码创建两个命令按钮

filtering All EXCEPT PO

过滤除了阿宝

    Private Sub CommandButton1_Click()
       Call Except_PO

     End Sub

Filtering data starting with PO and contains OH

从PO开始过滤数据,包含OH

   Private Sub CommandButton2_Click()
       Call POwithOH

     End Sub

So if you click CommandButton1, your pivot will filter those data that don't start with PO. And when you click CommandButton2, your pivot will filter all data that starts with PO AND contains OH.

所以如果你点击CommandButton1,你的主元会过滤掉那些不从PO开始的数据。当您单击CommandButton2时,您的pivot将过滤所有以PO开头并包含OH的数据。

#2


0  

If I'm understanding the conditions correctly, this should get you the results you want for the first case...

如果我理解正确的话,这应该能让你得到你想要的第一种情况的结果……

Show All Items except ones that begin with "PO" :

显示除以“PO”开头的项目外的所有项目:

Sub ShowAllButPO()

    Dim ws As Worksheet
    Dim pvtTable As PivotTable
    Dim pvtField As PivotField
    Dim pvtItem As PivotItem

    Set ws = ActiveSheet
    Set pvtTable = ws.PivotTables("PIVOT1")
    Set pvtField = pvtTable.PivotFields("Invoicenr")

    pvtTable.RefreshTable

    pvtTable.ClearAllFilters

    For Each pvtItem In pvtField.PivotItems
        If Left(UCase(pvtItem), 2) = "PO" Then
            pvtItem.Visible = False
        End If
    Next

End Sub

And this should cover the second condition...

这应该包括第二个条件……

Show All Items in "invoicenr" that start with "PO" and also contain "OH" :

显示“invoicenr”中以“PO”开头并包含“OH”的所有项目:

Sub ShowOnlyPO()

    Dim ws As Worksheet
    Dim pvtTable As PivotTable
    Dim pvtField As PivotField
    Dim pvtItem As PivotItem

    Set ws = ActiveSheet
    Set pvtTable = ws.PivotTables("PIVOT1")
    Set pvtField = pvtTable.PivotFields("Invoicenr")

    pvtTable.RefreshTable

    pvtTable.ClearAllFilters

    For Each pvtItem In pvtField.PivotItems
        If Left(UCase(pvtItem), 2) = "PO" And InStr(UCase(pvtItem), "OH") > 0 Then
            pvtItem.Visible = True
        Else
            pvtItem.Visible = False
        End If
    Next

End Sub

I'm less sure about what you wanted for the second condition. Your wording "i want to see Everything else and the invoicenr that starts with PO and contains "OH"" wasn't completely clear to me.

我不太确定你对第二个条件想要什么。你说的“我想看看其他的东西,发票以PO开头,包含“OH”,我不是很清楚。

If you could clarify what you mean by "Everything else and invoicenr that starts with PO.. etc etc" then I can update my code if needed.

如果你能阐明你所说的“其他一切和以PO开头的发票”是什么意思。等等,如果需要的话,我可以更新我的代码。

Also, if those two code blocks end up getting you what you want, then you could just assign each macro to its own button in your worksheet. That way, you could just toggle the two scenarios without having to open the VBEditor to run the code. If you are unsure how to do this, check out this link

另外,如果这两个代码块最终得到了您想要的结果,那么您可以在工作表中为每个宏分配它自己的按钮。这样,您只需切换这两个场景,而不必打开VBEditor来运行代码。如果你不知道怎么做,看看这个链接


推荐阅读
  • IhaveatableinAccess2003VBAwith164columnsbutthedataIgethas181columnanditispossi ... [详细]
  • IamusingaUserForminExceltomovecontentfromtextbox1tofirstemptyrowonsheet2.Belowc ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • Iamtryingtocreateanarrayofstructinstanceslikethis:我试图创建一个这样的struct实例数组:letinstallers: ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • vb.net面试题,请大家帮忙,谢谢。如果需要讲详细一点,那就加我QQ531412815第4题,潜在的错误,这里的错误不是常规错误,属于那种只有在运行是才知道的错误:Catchex ... [详细]
  • 你是否注意到,当你在高优先级下运行应用程序是,应用程序运行得特别快(这也视你运行什么样的应用程序而定)。拿WinRar做例子 ... [详细]
  • enterimagedescriptionhere ... [详细]
author-avatar
查建樺
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有