使用VBA过滤数据透视表中包含特定文本的项目

时间:2017-03-21 14:08:56

标签: excel vba excel-vba filter pivot-table

我一直在尝试构建代码来过滤数据透视表中包含特定文本片段的所有项目。我最初想象我可以使用星号(*)来表示我的文本之前或之后的任何字符串,但VBA将其作为字符读取。这是在Userform列表框中显示数据透视表数组所必需的。看看我尝试了什么:

<head>
    <link rel="stylesheet" href="css/animations.css" type="text/css"/>
    </head>
    <script src="js/css3-animate-it.js"></script>
    
         <div class="work__container work__container--mobile">
		<div class="work__flex">
			<div class="work__flex--item hvr-amur animatedParent">
				<div class="amur full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">AMUR</h3>
						<p class="overlay__text--sub">Materials making a difference.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hvr-pop animatedParent">
				<div class="pop-shoes full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">POP SHOES</h3>
						<p class="overlay__text--sub">Walk happy.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hvr-linens animatedParent">
				<div class="love-your-linens full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">LOVE YOUR LINENS</h3>
						<p class="overlay__text--sub">Luxury bedding made easy.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hidden hvr-bench animatedParent">
				<div class="bench full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">BENCH</h3>
						<p class="overlay__text--sub">Global retail vs. holiday shopping.</p>
					</div>
				</div>
			</div>
		</div>
		<div class="work__flex">
			<div class="work__flex--item duplicate hvr-bench animatedParent">
				<div class="bench full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">BENCH</h3>
						<p class="overlay__text--sub">Global retail vs. holiday shopping.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hvr-dogpack animatedParent">
				<div class="dogpack full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">DOGPACK</h3>
						<p class="overlay__text--sub">The pack designed for the urban dog.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hvr-smokeshow animatedParent">
				<div class="smoke-show full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">SMOKE SHOW</h3>
						<p class="overlay__text--sub">Lightly smoked jalape&ntilde;o hot sauce.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hidden hvr-coffee animatedParent">
				<div class="roman-coffee-co full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">ROMAN COFFEE</h3>
						<p class="overlay__text--sub">Beans, love & craftsmanship.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hidden hvr-protech animatedParent">
				<div class="protech full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">PROTECH</h3>
						<p class="overlay__text--sub">Global-scale powder & liquid applications.</p>
					</div>
				</div>
			</div>
		</div>
		<div class="work__flex">
			<div class="work__flex--item duplicate hvr-coffee animatedParent">
				<div class="roman-coffee-co full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">ROMAN COFFEE</h3>
						<p class="overlay__text--sub">Beans, love & craftsmanship.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item duplicate hvr-protech animatedParent">
				<div class="protech full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">PROTECH</h3>
						<p class="overlay__text--sub">Global-scale powder & liquid applications.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hvr-northstone animatedParent">
				<div class="northstone full work_overlay animated pulse animate-this">
					<div class="overlay__text">
						<h3 class="overlay__text--header">NORTHSTONE</h3>
						<p class="overlay__text--sub">Love the land you live on.</p>
					</div>
				</div>
			</div>
			<div class="work__flex--item hidden item__arrow">
				<div class="arrow animate__arrow">
					<a href="#services-wrap" class="smooth-scroll">
                                             <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64.57 80" class="work--grid--arrow"><defs><style>.cls-arrow{fill:#444444;}</style></defs><title>Asset 6</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-arrow" d="M28.28,4V66.34L6.83,44.89a4,4,0,1,0-5.66,5.66L29.46,78.83h0a4,4,0,0,0,.61.5c.1.07.21.11.31.17a3.86,3.86,0,0,0,.37.2,3.65,3.65,0,0,0,.41.13c.11,0,.22.08.33.1a4,4,0,0,0,.79.08h0a4,4,0,0,0,.77-.08c.12,0,.23-.07.35-.1a3.58,3.58,0,0,0,.4-.12,4,4,0,0,0,.4-.21c.1-.05.2-.1.29-.16a3.88,3.88,0,0,0,.61-.5L63.39,50.54a4,4,0,1,0-5.66-5.66L36.28,66.34V4a4,4,0,0,0-8,0Z"/></g></g></svg>
					</a>
				</div>
			</div>
		</div>
	</div>
	
<script>
	
	$(document).ready(function() {

    var windowSize = $(window).outerWidth();

    if ( windowSize <= 768 ) {

        $(window).on("scroll", function() {

            $(".animate-this").each(function() {

                var documentTop = $(window).scrollTop();
                var documentBottom = documentTop + $(window).height();
                var elementTop = $(this).offset().top;
                var elementBottom = elementTop + $(this).height();

                if ( (elementTop >= documentTop) && (elementBottom <= documentBottom) ) {

                    $(this).addClass("my-animation");
                    $(this).removeClass("animate-this");

                }

            });

        });

    }

});

</script>

3 个答案:

答案 0 :(得分:2)

尝试使用以下代码过滤从Like收到的*外卡[{1}}和字符f字段“过滤连接”字段中的所有项目。

InputBox

答案 1 :(得分:0)

您可以通过以下方式调整Shai的答案,以显着加快速度:

  1. 删除IF不需要的TRUE分支
  2. 在代码执行时将ManualUpdate设置为TRUE,以停止 每次更改可见时重新计算数据透视表 任何PivotItems的状态。
  3. 关闭屏幕更新和计算(如果有 直到完成后,工作簿中的易失性函数

    如果您希望比较不区分大小写,您可能还想在其中放置Option CompareText。

  4. 如果用户输入数据透视表中不存在的内容,您可能需要进行一些错误处理。

    你可能想要给我的blogpost on this stuff一个读取,因为数据透视表的过滤速度非常慢,并且它讨论了许多加快速度的方法

    这是Shai代码的重写例子:

    Option Explicit
    Option Compare Text
    
    Sub FilterCstomers()
    
        Dim pt  As PivotTable
        Dim pf  As PivotField
        Dim pi  As PivotItem
        Dim f   As String
    
        f = InputBox("Type the text you want to filter:")
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
    
        Set pt = Sheets("Customers Pivot").PivotTables("Customers_PivotTable")
        Set pf = pt.PivotFields("Concatenation for filtering")
        pt.ManualUpdate = True
        With pf
            .ClearAllFilters
            On Error GoTo ErrHandler
            For Each pi In .PivotItems
                If Not pi.Name Like "*" & f & "*" Then
                    pi.Visible = False
                End If
            Next pi
    
        End With
    ErrHandler:
            If Err.Number <> 0 Then pf.ClearAllFilters
            pt.ManualUpdate = False
            On Error GoTo 0
            With Application
                .ScreenUpdating = True
                .Calculation = xlCalculationAutomatic
            End With
        End Sub
    

答案 2 :(得分:0)

为什么不只是:

.PivotFields(“ PivotFieldName”)。PivotFilters.Add2类型:= xlCaption包含,值1:=“ X”