ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   'equals' for non-numeric data (https://www.excelbanter.com/excel-programming/371329-equals-non-numeric-data.html)

Josie[_2_]

'equals' for non-numeric data
 
Hi all

I'm very very new to this, so I'm writing a very simple macro. What I want
it to do is compare two cells and see if the contents are the same. The
contents are non numeric however.

Is there a 'is the same as' function or something?

Thanks, J

Tom Ogilvy

'equals' for non-numeric data
 
the equal sign works for non-numeric data as well

if Range("A1").Value = Range("B9").value then

However, it has to be an exact match.

if lcase(Range("A1").Value) = lcase(Range("B9").value) then


You can use strcomp

if strcomp(Range("A1").Value,Range("b9").value,vbText Compare) = 0 then

the vbTextcompare says to use a case insensitive evaluation.

--
Regards,
Tom Ogilvy


"Josie" wrote:

Hi all

I'm very very new to this, so I'm writing a very simple macro. What I want
it to do is compare two cells and see if the contents are the same. The
contents are non numeric however.

Is there a 'is the same as' function or something?

Thanks, J


Josie[_2_]

'equals' for non-numeric data
 
Wicked! That works. Many thanks.

"Tom Ogilvy" wrote:

the equal sign works for non-numeric data as well

if Range("A1").Value = Range("B9").value then

However, it has to be an exact match.

if lcase(Range("A1").Value) = lcase(Range("B9").value) then


You can use strcomp

if strcomp(Range("A1").Value,Range("b9").value,vbText Compare) = 0 then

the vbTextcompare says to use a case insensitive evaluation.

--
Regards,
Tom Ogilvy


"Josie" wrote:

Hi all

I'm very very new to this, so I'm writing a very simple macro. What I want
it to do is compare two cells and see if the contents are the same. The
contents are non numeric however.

Is there a 'is the same as' function or something?

Thanks, J


Andrew Taylor

'equals' for non-numeric data
 
Josie, try this:

If Range("A1").Value = Range("A2").Value Then
MsgBox "A1 and A2 have the same value"
Else
MsgBox "A1 and A2 have different values"
End if


Come to think of it, it depends on what exactly you mean
by the "same contents". For example if A1 is 2 and
A2 is =1+1 are they the same or different? If you want
to count them as be different use .Formula instead of
..Value. If you want to compare their appearance (e.g.
the same number with two different formats) use .Text

hope this helps
Andrew


Josie (DONOTSPAM) wrote:
Hi all

I'm very very new to this, so I'm writing a very simple macro. What I want
it to do is compare two cells and see if the contents are the same. The
contents are non numeric however.

Is there a 'is the same as' function or something?

Thanks, J



Tom Ogilvy

'equals' for non-numeric data
 
For example if A1 is 2 and
A2 is =1+1 are they the same or different?


What would you consider as numeric? <g

--
Regards,
Tom Ogilvy

"Andrew Taylor" wrote:

Josie, try this:

If Range("A1").Value = Range("A2").Value Then
MsgBox "A1 and A2 have the same value"
Else
MsgBox "A1 and A2 have different values"
End if


Come to think of it, it depends on what exactly you mean
by the "same contents". For example if A1 is 2 and
A2 is =1+1 are they the same or different? If you want
to count them as be different use .Formula instead of
..Value. If you want to compare their appearance (e.g.
the same number with two different formats) use .Text

hope this helps
Andrew


Josie (DONOTSPAM) wrote:
Hi all

I'm very very new to this, so I'm writing a very simple macro. What I want
it to do is compare two cells and see if the contents are the same. The
contents are non numeric however.

Is there a 'is the same as' function or something?

Thanks, J





All times are GMT +1. The time now is 07:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com