Excel 需要比较两列并向第三列提供输出
我想写的VBA代码,将比较两个不同的表中的两列 我在Excel 需要比较两列并向第三列提供输出,excel,vba,Excel,Vba,我想写的VBA代码,将比较两个不同的表中的两列 我在Sheet1列B和Sheet2列B中有数据 比较这两列的公式位于表2:=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
Sheet1
列B
和Sheet2
列B
中有数据
比较这两列的公式位于表2
:=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
如果要比较单元格的显示/格式化文本而不是其后面的值,请使用.text而不是.value(例如,“2019年9月10日”而不是43718)