创build一个循环来比较Excel中的两个单元格

我写了一些代码来比较两个单元格。 此时代码将D2与J2进行比较。 我需要比较D3与J3,D4与J4等。我知道最简单的方法来做到这一点是一个循环,但不能得到它的工作。 任何帮助深表感谢。

这是迄今为止的代码:

Public Sub Practice1() Dim UpLim As Double, LowLim As Double Dim outcome As String UpLim = Range("d2").Value LowLim = Range("j2").Value If UpLim > LowLim Then result = "Headroom" Else result = "NoHeadroom" End If Range("e2").Value = result End Sub 

这是一个典型的循环:

 Public Sub Practice1() Dim UpLim As Double, LowLim As Double Dim outcome As String, i As Long For i = 2 To 10 UpLim = Range("d" & i).Value LowLim = Range("j" & i).Value If UpLim > LowLim Then result = "Headroom" Else result = "NoHeadroom" End If Range("e" & i).Value = result Next i End Sub 

select我的限制,以满足您的需求。

轻微的替代方法

 Public Sub Practice1() dim result as string dim x as integer for x = 2 to 10 If (cells(x,4) > cells(x,10)) Then cells(x,5)= "Headroom" Else cells(x,5)= "NoHeadroom" End If next End Sub