[英]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.