私は実際、これに対する答えとして見つけるのに非常に苦労していることに非常に驚いています。(同じワークシート上に)たくさんの数字を含む2つの列があります。列の各行に対して「列1の値>列2の値の場合はこれを実行する」というコードが必要です。私は試した
If sheet.range("B2:B35").Value > sheet.range("C2:C35").Value Then
'do something
End If
しかし、どうやらそれはそのようには機能しません。
各行を他の行とは独立してチェックするためのループについて考える必要があります。
アイデアは次のようなものです。
For i = 2 to 35
If Sheet.Range("B" & i).Value > Sheet.Range("C" & i).Value
'Do Something for Row i
End If
Next
Value
は暗黙的であるため省略できます。つまり、Sheet.Range("B" & i).Value
と同じ結果が返されます。Sheet.Range("B" & i)
さらに、ニーズに応じてセルに対処する方法は多数あります。
Range() 'Can be used to address multiple cells at the same time over a range
' or used to address a single cell as is done here
Cells() 'Can be used to address a single Cell by calling Cells("B" & i) as is
' done above, or can reference the row and column numerically like
' Cells(2, i)
Offset()
また、上記のいずれかの方法は、次のような特定のワークシート内を移動する場合と組み合わせて使用できます。
Cells(2, 1).Offset(0, i) 'Addresses the Cell offset from "B1" by 0 columns and
' i rows.
Cells(2, i)
私は個人的にこれらの場合に使用する傾向がありRange
ますが、サンプルコードスニペットから直接借用したので使用しました。