PC Review


Reply
Thread Tools Rate Thread

Comparing Text Contents of Cells

 
 
Keith Norris
Guest
Posts: n/a
 
      16th Dec 2008
Please tell me how to compare the contents of cells that contain text. If I
try to compare a cell that contains two identical strings, they never equal
each other. For example, if I try a statement similiar to the following:

If Cells(1,1) = Cells(2,1) Then
msgbox "They are equal"
End If

I have never been able to get this to result in a true condition meaning
They are equal is never shown in a messagebox when the two cells contain for
example EQUAL. I have also tried using the range object with no success. I
know this must be able to be done but I must not be using the correct syntax.

Please reply with the correct syntax and an example.

Thank you very much in advance!




--
Thank You,
Keith
 
Reply With Quote
 
 
 
 
Luke M
Guest
Posts: n/a
 
      16th Dec 2008
If Cells(1, 1).Value = Cells(2, 1).Value Then
MsgBox "They are equal"
End If
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Keith Norris" wrote:

> Please tell me how to compare the contents of cells that contain text. If I
> try to compare a cell that contains two identical strings, they never equal
> each other. For example, if I try a statement similiar to the following:
>
> If Cells(1,1) = Cells(2,1) Then
> msgbox "They are equal"
> End If
>
> I have never been able to get this to result in a true condition meaning
> They are equal is never shown in a messagebox when the two cells contain for
> example EQUAL. I have also tried using the range object with no success. I
> know this must be able to be done but I must not be using the correct syntax.
>
> Please reply with the correct syntax and an example.
>
> Thank you very much in advance!
>
>
>
>
> --
> Thank You,
> Keith

 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      16th Dec 2008
Keith,

It depends on what you actually want to compare: the text (displayed value) or the value
(underlying, unformatted value)

Sub TryNow()
If Cells(1, 1).Value = Cells(2, 1).Value Then
MsgBox "The values are equal"
Else
MsgBox Cells(1, 1).Value & " does not equal " & Cells(2, 1).Value
End If
If Cells(1, 1).Text = Cells(2, 1).Text Then
MsgBox "The text values are equal"
Else
MsgBox Cells(1, 1).Text & " does not equal " & Cells(2, 1).Text
End If
End Sub


HTH,
Bernie
MS Excel MVP


"Keith Norris" <(E-Mail Removed)> wrote in message
news:00F4629D-4A5E-45CE-9175-(E-Mail Removed)...
> Please tell me how to compare the contents of cells that contain text. If I
> try to compare a cell that contains two identical strings, they never equal
> each other. For example, if I try a statement similiar to the following:
>
> If Cells(1,1) = Cells(2,1) Then
> msgbox "They are equal"
> End If
>
> I have never been able to get this to result in a true condition meaning
> They are equal is never shown in a messagebox when the two cells contain for
> example EQUAL. I have also tried using the range object with no success. I
> know this must be able to be done but I must not be using the correct syntax.
>
> Please reply with the correct syntax and an example.
>
> Thank you very much in advance!
>
>
>
>
> --
> Thank You,
> Keith



 
Reply With Quote
 
Socko
Guest
Posts: n/a
 
      16th Dec 2008
If you are sure that the content of a cell is equal in terms of string
or value, formating, formula, etc. then this may work... If you want
to just find out if they are equal in content string regardless of
letter case, you may use ...

If ucase$(Cells(1, 1).text) = ucase$(Cells(2, 1).text) Then
MsgBox "They are equal"
End If

If you jsut want to compare value then you may use the following:

If Cells(1, 1).Value = Cells(2, 1).Value Then
MsgBox "They are equal"
End If

I hope this helps.

Selva V Pasupathy
For more on Excel, VBA, and other resources
Please visit: http://socko.wordpress.com
 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      16th Dec 2008
Luke,

Value is the default property addressed when none is specified for range objects, so these two are
essentially identical,,though the first is the better programming practice:

If Cells(1, 1).Value = Cells(2, 1).Value Then
If Cells(1,1) = Cells(2,1) Then

HTH,
Bernie
MS Excel MVP


"Luke M" <(E-Mail Removed)> wrote in message
news:52F7D2F2-8779-4347-B2BA-(E-Mail Removed)...
> If Cells(1, 1).Value = Cells(2, 1).Value Then
> MsgBox "They are equal"
> End If
> --
> Best Regards,
>
> Luke M
> *Remember to click "yes" if this post helped you!*
>
>
> "Keith Norris" wrote:
>
>> Please tell me how to compare the contents of cells that contain text. If I
>> try to compare a cell that contains two identical strings, they never equal
>> each other. For example, if I try a statement similiar to the following:
>>
>> If Cells(1,1) = Cells(2,1) Then
>> msgbox "They are equal"
>> End If
>>
>> I have never been able to get this to result in a true condition meaning
>> They are equal is never shown in a messagebox when the two cells contain for
>> example EQUAL. I have also tried using the range object with no success. I
>> know this must be able to be done but I must not be using the correct syntax.
>>
>> Please reply with the correct syntax and an example.
>>
>> Thank you very much in advance!
>>
>>
>>
>>
>> --
>> Thank You,
>> Keith



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing contents in three cells gcotterl Microsoft Excel Worksheet Functions 1 10th Sep 2010 02:04 AM
Comparing contents in three cells gcotterl Microsoft Excel Worksheet Functions 0 10th Sep 2010 01:36 AM
Comparing cells written in text to cells producing text from a for David B Microsoft Excel Programming 1 28th Apr 2008 07:20 PM
Comparing cell contents with different reference cells Martin B Microsoft Excel Worksheet Functions 3 22nd Nov 2006 07:10 PM
Comparing two coloumns text contents & extract different values =?Utf-8?B?bmFiaWw=?= Microsoft Excel Programming 0 12th Jan 2006 01:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:15 AM.