需要比较两列并提供输出到第三列



我想编写VBA代码来比较两个不同工作表中的两列。

我在BSheet1列和Sheet2B中有数据。

比较两列的公式在Sheet2=B2=Sheet1!B2中。

你能帮我为上面的公式编写VBA代码吗?

我不确定如何在VBA代码中使用上述公式。

要比较的基本代码是

If Sheet1.Range("B1").Value = Sheet2.Range("B1").Value Then
'Code to execute when criteria is met
Else
'Code to execute when criteria is not met
End If

else 部分是可选的,如果您不需要,可以省略

如果要比较整个列,有几种方法可以做到这一点。 我最喜欢的是:

Dim iLastRow As Integer
iLastRow = Sheet1.Cells(Sheet1.Rows.Count, 2).End(xlUp) 'Gets the last row
For i = 1 To iLastRow 'Compares each row and executes the code if 
If Sheet1.Range("B" & i).Value = Sheet2.Range("B" & i).Value Then
'Code to execute when criteria is met
Else
'Code to execute when criteria is not met
End If
Next i

如果要比较单元格的显示/格式化文本,而不是其后面的值,请使用。文本而不是。值(例如"2019 年 9 月 10 日"而不是 43718(

最新更新