繁体   English   中英

在 Excel VBA 中使用“不等于”运算符

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM