简体   繁体   English

遍历过滤器中的枢轴项时,vba SpecialCells(xlCellTypeVisible)无法正常工作

[英]vba SpecialCells(xlCellTypeVisible) not working correctly when looping through pivot items in a filter

I am using the below code to cycle through items in a filter in a pivot table and then copy the filtered results. 我正在使用以下代码在数据透视表中的过滤器中的各项之间循环,然后复制过滤后的结果。

But for some reason the code fails to account for the items that have no results and instead appears to copy the previous results instead. 但是由于某种原因,代码无法说明没有结果的项目,而是代替了以前的结果。

I have error handling in the code for the items that will show no results but it just doesn't seem to work - when walking through the code it skips the 'if' I have placed in there to catch the 'nothing'. 我在项目的代码中有错误处理,这些项目将不会显示任何结果,但似乎无法正常工作-在遍历代码时,它会跳过我放置在其中的“如果”以捕获“无”的内容。

It then (when it gets to the copying/pasting part of the code) copies the (now hidden) cells that were visible under the last item selected in the filter on the previous loop. 然后,它(当到达代码的复制/粘贴部分时)复制在上一个循环的过滤器中最后选择的项目下可见的(现已隐藏)单元格。

I have missed off the end of the code as it gets a bit lengthy and it all works, it's just the bit with the visible cells I can't seem to get working. 我错过了代码的结尾,因为它有点冗长,而且一切正常,这只是可见单元格的一点点,我似乎无法正常工作。

Sub Bulletin_Chase()

Dim pt As PivotTable
Dim pi As PivotItem
Set pt = Worksheets("By User").PivotTables("PivotTable1")

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

For Each pi In pt.PageFields("Supplier").PivotItems
  pt.PageFields("Supplier").CurrentPage = pi.Name

With Range("Filters")
    .AutoFilter Field:=2, Criteria1:=Array("false"), Operator:=xlFilterValues
End With

On Error Resume Next
Set rng = Worksheets("By User").Range("Bulletin_List").SpecialCells(xlCellTypeVisible)

If rng Is Nothing Then
    Worksheets("By User").AutoFilterMode = False
    Range("Filters").AutoFilter
    GoTo Continue
End If

etc... 等等...

Really struggling with this so any help at all from you geniuses would be greatly appreciated. 真的为此感到挣扎,因此,非常感谢您的才华。

When trying to set rng, if there are no visible cells, then the assignment fails and it skips to the next line of code. 尝试设置rng时,如果没有可见的单元格,则分配失败,并跳至下一行代码。 This process does not set rng to Nothing. 此过程不会将rng设置为Nothing。 It just does nothing, which leaves rng with the value it had from the last time the loop ran. 它什么也不做,这使rng保留了自上次循环运行以来的值。

The simplest solution is to force rng to be nothing just before trying to assign it to visible cells. 最简单的解决方案是在尝试将rng分配给可见单元之前将其强制为空。 That way, if the assignment fails, you will successfully detect the failure when you test for rng = nothing: 这样,如果分配失败,则在测试rng = none时将成功检测到失败:

Range("Filters").AutoFilter Field:=2, Criteria1:=Array("false"), Operator:=xlFilterValues 'No need for a With block if you're only performing 1 method

set rng = Nothing 'Clear the rng variable before trying to assign a new range to it
On Error Resume Next
Set rng = Worksheets("By User").Range("Bulletin_List").SpecialCells(xlCellTypeVisible)
On Error Goto 0 'Restore normal On Error behaviour to trap any other unexpected errors

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SpecialCells(xlCellTypeVisible)在UDF中不起作用 - SpecialCells(xlCellTypeVisible) not working in UDF 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) sheet.SpecialCells(xlCellTypeVisible)中的范围也选择了空白行,尽管应用了VBA过滤器 - range in sheet.SpecialCells(xlCellTypeVisible) also selecting blank rows although a filter was applied VBA VBA:处理过滤的行和SpecialCells(xlCellTypeVisible)与将数据复制到新表中 - VBA: Working with filtered rows and SpecialCells(xlCellTypeVisible) vs copying data into new sheet Selection.SpecialCells(xlCellTypeVisible).Count 在过滤器产生单行结果时给出溢出错误 - Selection.SpecialCells(xlCellTypeVisible).Count gives overflow error when single row result from filter 使用.SpecialCells(xlCellTypeVisible) 属性循环时发出隐藏单元格 - Emit hidden cell while looping using .SpecialCells(xlCellTypeVisible) property VBA通过.SpecialCells(xlCellTypeVisible)合并自动过滤的单元格 - VBA merging autofiltered cells via .SpecialCells(xlCellTypeVisible).Range 使用.Hidden或.SpecialCells(xlCellTypeVisible)来忽略隐藏的行 - 不工作 - Using .Hidden or .SpecialCells(xlCellTypeVisible) to Ignore Hidden Rows — Not Working 尽管使用了`SpecialCells(xlCellTypeVisible)`,但选择可见单元格不起作用 - Selecting Visible Cells is not working although using `SpecialCells(xlCellTypeVisible)`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM