此宏在列 B 中搜索“检查”数字 (IsNumeric
),当找到时将数字偏移 (0,2),然后用“检查”替换原始单元格中的数字。
当作为规则使用 lastrow
时,它运行得非常慢(尽管如果我将范围设置为整个列,即“B:B”,速度同样慢)。
这两种不同方法之间的唯一区别是 lastrow
规则将“Checks”添加到最后一行,一直沿着列一直向下到工作表的最后一行。我整天都在黑客攻击,通常这样的事情不会让我陷入困境(双关语)。
Sub Move_Checks()
Dim ws1 As Worksheet
Set ws1 = Sheets("Combined")
Dim lastrow As Long
lastrow = Cells(Rows.Count, "a").End(xlUp).row
Dim rng1 As Range
Set rng1 = Range("B:B")
Dim Cell As Range
Set Cell = Range("B2" & lastrow)
For Each Cell In rng1
If IsNumeric(Cell.value) = True Then
Cell.Offset(0, 3) = Cell.value
End If
Next Cell
For Each Cell In rng1
If IsNumeric(Cell.value) = True Then
Cell.value = "Check"
End If
Next Cell
End Sub
最佳答案
尝试修改后的代码。
您不需要两个循环,您可以一次完成更改。
您的 rng1
变量被设置为整个 B 列。
您几乎将 Cell
变量设置为实际上是正确的 Range
,但随后在遍历 时通过将它用作每个单元格的占位符来覆盖它>范围
。
Sub Move_Checks()
Dim ws1 As Worksheet
Dim rng1 As Range
Dim Cell As Range
Dim lastrow As Long
Set ws1 = Sheets("Combined")
lastrow = ws1.Cells(Rows.Count, "a").End(xlUp).row
Set rng1 = Range("B2:B" & lastrow)
For Each Cell In rng1
If IsNumeric(Cell.value) = True Then
Cell.Offset(0, 3) = Cell.value
Cell.value = "Check"
End If
Next Cell
End Sub
关于excel - 计算机因简单代码而窒息。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62566797/