如果我的理解正确,您正在尝试创建一个循环(您的示例专门使用单元格 E8)并且不确定如何实现这一点。遍历行的基本 For 循环的结构如下:
Dim i as Long, lr as Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 8 to lr Step 1
'Do something using i
Next i
我们现在可以修复您的代码以利用 i,这样:
Sheets("Tool").Range("E" & i).Value = ""
或
Sheets("Tool").Cells(i, "E").Value = ""
看着你的例子,我有点困惑......什么是oCell(具体来说是哪一列),输出应该在哪里?
从您的示例中,我看起来正在评估 E7 并且输出在 E8 中...您是遍历列还是向下行?需要有关您的源数据的更多信息。
编辑:
Dim i as Long, lr as Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 8 to lr Step 2
If len(Cells(i-1,5)) < 52 AND len(Cells(i-1,5)) <> 0 then
Sheets("Tool").Cells(i,5).Value = ""
ElseIf len(Cells(i-1,5)) > 52 then
Sheets("Tool").Cells(i,5).Value = "Exceeding"
End If
Sheets("Tool").Cells(i,5).Value = "Cannot be blank"
Next i
编辑 2:此代码向下 E 列
Dim i As Long, lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 7 To lr Step 2
Select Case Len(Cells(i, 5))
Case 0
Cells(i + 1, 5).Value = "Cannot be blank"
Case Is < 52
Cells(i + 1, 5).Value = ""
Case Is > 52
Cells(i + 1, 5).Value = "Exceeded"
End Select
Next i
编辑 3:此代码在第 7 行正确
Dim i As Long, lc As Long
lc = Cells(5, Columns.Count).End(xltoLeft).Column
For i = 4 To lc Step 1 'looks like you're going 1 by 1 across row 7
Select Case Len(Cells(7, i))
Case 0
Cells(8, i).Value = "Cannot be blank"
Case Is < 52
Cells(8, i).Value = ""
Case Is > 52
Cells(8, i).Value = "Exceeded"
End Select
Next i
编辑 4:此代码向下并向右
Dim i As Long, j As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 4).End(xlUp).Row 'Based on last row in column D
'lc = Cells(5, Rows.Count).End(xlToLeft).Column 'Based on last column in row 5 'Not needed, commented out, known end in Column 68
For i = 7 To lr Step 2
For j = 4 to 68 Step 1 'From D (4) to BP (68)
Select Case Len(Cells(i, j))
Case 0
Cells(i + 1, j).Value = "Cannot be blank"
Case Is < 52
Cells(i + 1, j).Value = ""
Case Is > 52
Cells(i + 1, j).Value = "Exceeded"
End Select
Next j
Next i