简体   繁体   中英

Check if one cell contains the EXACT same data as another cell VBA

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

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]"

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"

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"

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.

Try using Strcomp()

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

When StrComp is 0, it means an exact match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM