Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    In a macro I want to compare 2 cells and check if the data in Cell one is somewhere in Cell two.

    eg.

    Cell One = "Cat"
    Cell Two = "The Cat sat on the mat"

    Which command is the best.

    Regards

    Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Let's say that the cells are A1 and B1. You can use

    Code:
    If Instr(Range("B2"), Range("A1")) > 0 Then
      MsgBox "Yes"
    Else
      MsgBox "No"
    End If
    The comparison is case-sensitive by default, e.g. if A1 = "cat", the message box will display "No".
    If you want the comparison to be case-insensitive, you can use InStr(1, Range("B1"), Range("A1"), vbTextCompare)

  3. #3
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='777803' date='01-Jun-2009 16:07']Let's say that the cells are A1 and B1. You can use

    Code:
    If Instr(Range("B2"), Range("A1")) > 0 Then
      MsgBox "Yes"
    Else
      MsgBox "No"
    End If
    The comparison is case-sensitive by default, e.g. if A1 = "cat", the message box will display "No".
    If you want the comparison to be case-insensitive, you can use InStr(1, Range("B1"), Range("A1"), vbTextCompare)[/quote]

    Thanks Hans,

    That has sorted it.

    Regards
    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •