Cell Equal To EXACT Value

Hi all,

prob a very simple question for you guys. How do you check if a cell equals an exact value of "Product"

I am currently using:

Cells(i, 1).Text = "PRODUCT"

though this is also picking up any cell which contains the word Product, such as cells which contain the word Production.

So how do you check for an exact value



Answer this question

Cell Equal To EXACT Value

  • Eileen Ewen

    During my experience with programming, i found that using the command to convert the value to Lower Cased characters would solve a lot of problems in a search function. Since it will be a user who's typing most of the values in a sheet or database, consistency is one of the things which will missing when it comes to the content.

    In short, i usually use the following code for my comparison:

    if lcase(Cells(x,y).value) = "mystring" then

    'do routine

    endif

    So, if i were to use the same sample as Jon, then the following results would appear:

    product = true
    Product = true
    Production = false

    If (at a later stage) you would like to search for results which would at least contain the search criteria within the cell, try using the instr() function.

    Example:

    if instr(cells(x,y).value, "Product") >0 then

    'do routine

    endif

    The results would be:

    product = false
    Product = true
    Production = true

    Combine that with InStr(LCase(cells(x,y), "product") and all 3 of the items will be true.

    Hope this helps.


  • johnacs

    thanks for help guys, all is working 100%
  • Michael Baxter

    Your problem statement is inconsistent. I put these values into A1:A3 of a new sheet:

    product
    Product
    Production

    I ran the following from the Immediate Window:

    For i=1 to 3 : Debug.Print Cells(i, 1).Address, Cells(i,1).Text, Cells(i,1).Text = "Product" : Next

    with the following result:

    $A$1     product    False
    $A$2     Product    True
    $A$3     Production  False

    So Cells(i, 1).Text = "Product" is true only if the cell contains exactly "Product".

    If the module has "Option Compare Text" in its declarations, then the test will be case-insensitive, but it should not think that "Production" = "Product".

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Tutorials and Custom Solutions
    http://PeltierTech.com
    _______
    


  • PatOvens

    i'm using:

    if cells(i,1).text = "Product" then

    rest of code here

    End If


  • Davids Learning

    What formula are you using to test this equality

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Tutorials and Custom Solutions
    http://PeltierTech.com
    _______
    


  • Cell Equal To EXACT Value