[英]Won't "Exit Do" when condition is thought to be met after looping
Loop is to continue until the filtering of the table results in no visible rows (Table has no blanks).循环是继续,直到表的过滤结果没有可见的行(表没有空白)。 Then it is supposed to exit.
然后它应该退出。
If it finds visible rows, it operates properly and performs the Else condition.如果它找到可见行,它就会正常运行并执行 Else 条件。 It should do this and then loop through again until the filtering returns no visible rows.
它应该这样做,然后再次循环,直到过滤返回不可见的行。 eg pVisible should eventually become nothing.
例如,pVisible 最终应该变成无。
What's happening: If it initially filters and pVisible is found to be not nothing it performs the else and loops but once the filtering returns no rows, it keeps performing the else when I am expecting it to Exit Do.发生了什么:如果它最初过滤并且发现 pVisible 不是什么它执行 else 并循环但是一旦过滤返回没有行,它会在我期望它退出 Do 时继续执行 else。
Do
Sheet8.Activate
' Independent Check and Add
With Sheet8.ListObjects("Table1")
.AutoFilter.ShowAllData
.Range.AutoFilter Field:=23, Criteria1:="0"
.Range.AutoFilter Field:=22, Criteria1:="Independent"
On Error Resume Next 'ignore error if no visible rows
Set pVisible = .DataBodyRange.SpecialCells(xlCellTypeVisible) 'ignore headers
On Error GoTo 0 'stop ignoring errors
End With
If pVisible Is Nothing Then
Exit Do
Else
With Worksheets("Roster").AutoFilter.Range
Range("A" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
Range(Selection, Selection.Offset(, 1)).Copy
Worksheets("Add Names").Activate
Worksheets("Add Names").Range("A3").PasteSpecial Paste:=xlPasteValues
Range("A3:B3").Select
Selection.AutoFill Destination:=Range("A3:B" & Range("C" & Rows.Count).End(xlUp).Row)
Range("A3:D" & Range("A3").End(xlDown).Row).Select
Selection.Copy
Worksheets("Committee Table").Activate
Sheet1.ListObjects("CredDB").AutoFilter.ShowAllData
Range("A14").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
End With
End If
Loop
When you bypass an error pVisible remains the same.当您绕过错误时,pVisible 保持不变。 It does not change to Nothing.
它不会更改为 Nothing。
A possible quick and dirty way is, just before On Error GoTo 0
.一种可能的快速而肮脏的方法是,就在
On Error GoTo 0
之前。
If err > 0 then Set pVisible = Nothing
If you rearrange the logic you need not test pVisible.如果你重新安排你不需要测试 pVisible 的逻辑。
If err > 0 then Exit Do
As indicated in a comment "The conventional approach is to put Set pVisible = Nothing
before On Error Resume Next
".正如评论中所指出的“传统方法是将
Set pVisible = Nothing
放在On Error Resume Next
之前”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.