如果... Then语句“如果值存在于指定的列” [英] If...Then statement for "if value is present in a specified column"
问题描述
我正在尝试写一个如果...然后
条件使得:
I'm trying to write an If...Then
condition such that:
如果Sheet1中单元格A3的值存在于Sheet2的列A中,那么...
If the value of cell A3 in Sheet1 is present in column A of Sheet2 then...
我已尝试这个代码,但它不工作(错误):
I have tried with this code but it doesn't work (Error) :
If Worksheets("Sheet1").Range("A3") = Worksheets("Sheet2").Range("A:A") Then
...
推荐答案
我不太确定你为什么在这里收到错误,但是你的 If..Then
不能按照你希望/期望的方式工作。
I'm not quite sure why you're getting an error here, but your If..Then
condition is definitely not working the way you're hoping/expecting it will.
您正在尝试将单一值(Sheet1中的A3值)与Sheet2的整列(A列)进行比较,这绝对不一样您可以使用 Range.Find $ c
You're trying to compare a single value (the value of A3 in Sheet1) to an entire column (column A in Sheet2), which is definitely not the same as looking to see if that value is in that column.
$ c>方法来确定一个值是否存在于给定的范围内,如下所示。
You can use the Range.Find
method to determine whether a value is present in a given range, as shown below.
Dim varFindThis As Variant
Dim rngLookIn As Range
varFindThis = Worksheets("Sheet1").Range("A3")
Set rngLookIn = Worksheets("Sheet2").Range("A:A")
If Not rngLookIn.Find(varFindThis, LookIn:=xlValues) Is Nothing Then
...
End If
请参阅这个MSDN页面,了解有关该功能的更多细节以及如何使用。
See this MSDN page for more details on the function and how it can be used.
这篇关于如果... Then语句“如果值存在于指定的列”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!