代码没有循环通过表 [英] Code not looping through sheets
问题描述
以下代码在第一页上删除太多,然后不循环到第二页?
The following code is deleting too much on first sheet and then not looping to second sheet?? Error appearing on .FindNext statement.
Sub FindAndExecute3()
Dim Loc As Range
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
With sh.UsedRange
Set Loc = .Cells.Find(What:="AUTO.WHSE.")
If Not Loc Is Nothing Then
Do Until Loc Is Nothing
Rows(ActiveCell.Row & ":" & (ActiveCell.Row + 2)).Delete
Set Loc = .FindNext(Loc)
Loop
End If
End With
Set Loc = Nothing
Next
End Sub
推荐答案
您必须完全符合您的范围
STRONG>。
此问题一次又一次显示(例如,此)。
You have to fully qualify your Range
s.
This issue shows up once and again (e.g., this).
这是什么意思?不要使用单元格
,范围
,行
或列
,而不指定他们属于哪个工作表
,除非你特别想这样做(甚至在这种情况下,明确地使用 ActiveSheet
提高了可读性,并减少了错误的几率,类似于使用 Option显式
)。
例如,替换
What does this mean? Do not use Cells
, Range
, Rows
or Columns
without specifying which Worksheet
they belong to, unless you specifically want to do that (and even in that case, explicitly using ActiveSheet
improves readability and reduces the chances of errors, similar to using Option Explicit
).
For instance, replace
Rows(ActiveCell.Row & ":" & (ActiveCell.Row + 2)).Delete
与
sh.Rows(sh.<something>.Row & ":" & (sh.<something>.Row + 2)).Delete
我假设范围
被删除跨度行
从与 sh
相关的数字取得的数字。
I am assuming the Range
to be deleted spans Row
numbers taken from something related to sh
.
注1 :在这种情况下,即使没有限定 Range
s,代码继续执行,错误也可能不被忽视,因为它产生有效的结果。在其他情况下,没有完全限定的代码 Range
s的代码将抛出一个错误(例如,像 sheet1.Range(Cells(...
,当
sheet1
不是 ActiveSheet
)。**
Note 1: In this case, even without qualifying Range
s, code continues execution and the mistake may go unnoticed, as it produces a valid result. In other cases, code without fully qualified Range
s will throw an error (e.g., with something like sheet1.Range(Cells(...
, when sheet1
is not the ActiveSheet
).**
注意2 :只有当工作表$ c时,您才可以使用
ActiveCell
$ c>它是在 ActiveSheet
[ MSDN ]。
Note 2: You can work with the ActiveCell
only when the Worksheet
that it is on is the ActiveSheet
[MSDN].
这篇关于代码没有循环通过表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!