检查一个单元格是否包含与另一个单元格VBA相同的数据

时间:2022-09-24 19:05:10

I have an Excel Sheet with Column A and a Column B filled with data.

我有一个包含A列和B列的Excel表格。

B    C       (columns)
1   1a       (row 1 is here)
2   3
3   4
4   4a
4a  5
5   5a
5a  6
6   6a
6a  6b
6b  10
10  11
11  12
12  13
13  14
14  15
15  16
16  16a
16a 16b
16b 16c
16c OUT
7   7a
7a  8
8   9
9   6

I want to say "If data in cell B7 equals the exact same data entered into cell O7, then execute statement. If it doesn't, go to next cell and retrieve that value, then [perform other statements]"

我想说“如果单元格B7中的数据等于单元格O7中输入的数据,那么执行语句。”如果没有,转到下一个单元格并检索该值,然后[执行其他语句]"

The problem with what I have tried, is that even if a cell has a "4" in it and I want to find the the cell starting with exactly "4a," it still starts with the cell that contains just "4" because there is a 4 in "4a"

我尝试过的问题是,即使一个单元格中有一个“4”,我想要找到一个从“4a”开始的单元格,它仍然从包含“4”的单元格开始,因为“4a”中有一个4

Example:

例子:

For x = 7 
    If Sheets("Sheet3").Cells(x, 2).Value = Sheets("Sheet3").Cells(7, "O").Value
        [execute statement]

If "O7" is set equal to "4a", it looks up the first value in B with a "4" in it instead of exactly "4a"

如果“O7”被设置为“4a”,它会在B中查找第一个值,而不是“4a”

Is there a special function or command to make my code find the exact value and not just the first cell with part of the value in it?

是否有一个特殊的函数或命令来让我的代码找到确切的值,而不仅仅是第一个具有部分值的单元格?

Thank you.

谢谢你!

1 个解决方案

#1


1  

Try using Strcomp()

试着用Strcomp()

If StrComp (Sheets("Sheet3").Cells(x,2).Value,Sheets("Sheet3").Cells(7,"O").Value) = 0 Then ...

如果StrComp (Sheets(“Sheet3”).Cells(x,2).Value,Sheets(“Sheet3”).Cells(7,“O”).Value) = 0,则…

When StrComp is 0, it means an exact match.

当StrComp为0时,它表示精确匹配。

#1


1  

Try using Strcomp()

试着用Strcomp()

If StrComp (Sheets("Sheet3").Cells(x,2).Value,Sheets("Sheet3").Cells(7,"O").Value) = 0 Then ...

如果StrComp (Sheets(“Sheet3”).Cells(x,2).Value,Sheets(“Sheet3”).Cells(7,“O”).Value) = 0,则…

When StrComp is 0, it means an exact match.

当StrComp为0时,它表示精确匹配。