Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 需要比较两列并向第三列提供输出_Excel_Vba - Fatal编程技术网

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

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

我想写的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 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)