我正在使用 For Each
循环遍历 2D 范围对象。默认情况下,此循环先循环列,然后循环行。我在一列中进行了检查,指示我是否应该处理该行。我发现,即使我不打算处理该行,程序仍然会循环遍历给定范围行中的每一列 - 这是非常低效的。
示例数据集的图像:
当前代码:
Sub ForEachLoop()
Dim ws As Worksheet
Dim myRng As Range, cell As Range
Dim strCheck As String
Set myRng = Selection 'COLUMN F:G IN IMAGE
For Each cell In myRng
strCheck = ws.Range("E" & cell.Row).Value
If strCheck = "PROCESS" Then
'do something
End If
Next cell
End Sub
我想知道是否有办法移动到范围中的下一行,而不是循环遍历该行中的每一列。 For
循环需要保留在 For Each
循环内,因为我正在对单元格进行颜色格式化。
最佳答案
两个循环:循环行,进行检查,然后循环行中的单元格。
Dim rw As Range
For Each rw in myRng.Rows
strCheck = ws.Range("E" & rw.Row).Value
If strCheck = "PROCESS" Then
Dim cell As Range
For Each cell in rw.Cells
'process
Next
End If
Next
关于excel - 如何将 'Skip' 转到每个范围的下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66458564/