很抱歉,我对此很陌生。我有这个VBA宏:
Sub ScoreTest()
Dim score As Integer, result As String
score = Range("D2").Value
If score >= Range("E2").Value Then
result = "pass"
Else
result = "fail"
End If
Range("F2").Value = result
End Sub
当然,这仅适用于第2行。我编辑了该宏以应用于第2行到第16行,但这只是我的复制粘贴,因此它看起来像这样:
Sub ScoreTest()
Dim score As Integer, result As String
score = Range("D2").Value
If score >= Range("E2").Value Then
result = "achieved"
Else
result = "failed"
End If
Range("F2").Value = result
score = Range("D3").Value
If score >= Range("E3").Value Then
result = "achieved"
Else
result = "failed"
End If
Range("F3").Value = result
score = Range("D4").Value
If score >= Range("E4").Value Then
result = "achieved"
Else
result = "failed"
End If
Range("F4").Value = result
...
End Sub
在这种情况下,我将使用哪个循环函数,而不是对每一行进行复制+粘贴宏操作?
答案 0 :(得分:1)
类似
Option Explicit
Sub ScoreTest()
Dim score As Long, result As String, i As Long
With ThisWorkbook.Worksheets("Sheet1")
For i = 2 To 16
score = .Cells(i, "D")
If score >= .Cells(i, "E") Then
result = "achieved"
Else
result = "failed"
End If
.Cells(i, "F") = result
Next
End With
End Sub