[英]using “Not equal” operator in Excel VBA
我目前正在研究一个小宏,如果以下列之一是不同的数字,它会插入一行。 我有以下代码,但不明白为什么这不起作用。
谁能指出我的错误?
Sub Button1_Click()
Dim rowCount As Integer
rowCount = 10
If cell(rowCount, 2) <> cell((rowCount + 1), 2) Then
Rows(rowCount).Insert
rowCount = rowCount + 2
ElseIf cell(rowCount, 2) = cell((rowCount + 1), 2) Then
rowCount = rowCount + 1
End Sub
cell
既没有定义也没有Set
...使用Cells
。.Value
以表明您正在比较值。Else
而不是ElseIf...
并包括End If
。If Cells(rowCount, 2).Value <> Cells((rowCount + 1), 2).Value Then
Rows(rowCount).Insert
rowCount = rowCount + 2
Else
rowCount = rowCount + 1
End If
PS 不要使用Integer
来跟踪行,而是使用Long
代替always 。 有关完整的详细信息,请参阅此线程。
PPS始终使用Option Explicit
,它会标记未声明的变量cell
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.