Kur*_*urt 1 excel vba compare function
我真的很惊讶我找到答案就这么麻烦了.我有2列包含一堆数字(在同一工作表上).我只想让代码说"如果第1列中的值>第2列中的值,请对列中的每一行执行此操作".我试过了
If sheet.range("B2:B35").Value > sheet.range("C2:C35").Value Then
'do something
End If
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用.
您需要考虑一个循环来独立检查每一行.
这个想法是这样的:
For i = 2 to 35
If Sheet.Range("B" & i).Value > Sheet.Range("C" & i).Value
'Do Something for Row i
End If
Next
Run Code Online (Sandbox Code Playgroud)
在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)
Run Code Online (Sandbox Code Playgroud)
Offset()如果您希望移动给定的工作表,可以使用上述任一方法,例如:
Cells(2, 1).Offset(0, i) 'Addresses the Cell offset from "B1" by 0 columns and
' i rows.
Run Code Online (Sandbox Code Playgroud)
我个人倾向于Cells(2, i)在这些情况下使用,但我只是Range直接从你的示例代码片段中借用它.