Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default TypeName function

Why does the last message box return FALSE when testing a cell for a string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default TypeName function

Try
MsgBox (LCase(TypeName(ActiveCell.Value)) = "string")

or of course simply change "string" to "String"

For this kind of test I would use

MsgBox VarType(ActiveCell.Value) = vbString

Regards,
Peter T


"ArthurJ" wrote in message
...
Why does the last message box return FALSE when testing a cell for a
string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default TypeName function

Hi,

Try this. Note a few less parenthesis

Sub Test()
'The active cell contains the string "ABC"
MsgBox ActiveCell.Value 'Displays 'ABC'
MsgBox TypeName(ActiveCell.Value) 'Displays 'String'
MsgBox TypeName(ActiveCell.Value) = TypeName("String") 'Displays 'FALSE'
End Sub

Mike

"ArthurJ" wrote:

Why does the last message box return FALSE when testing a cell for a string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default TypeName function

As others have pointed out, you needed to use "String", not "string".
Whenever you do a logical test against a String constant (text within quote
marks), you must always be wary of case sensitivity. Some functions (like
InStr or Replace) have optional arguments whereby you can make the
comparison case insensitive; but others (like TypeName) do not.

--
Rick (MVP - Excel)


"Mike H" wrote in message
...
Hi,

Try this. Note a few less parenthesis

Sub Test()
'The active cell contains the string "ABC"
MsgBox ActiveCell.Value 'Displays 'ABC'
MsgBox TypeName(ActiveCell.Value) 'Displays 'String'
MsgBox TypeName(ActiveCell.Value) = TypeName("String") 'Displays
'FALSE'
End Sub

Mike

"ArthurJ" wrote:

Why does the last message box return FALSE when testing a cell for a
string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default TypeName function

Along similar lines, to avoid case sensitivity with string comparisons could
head the module -

Option Compare Text

In general though best not to use that without specific reason.

Regards,
Peter T

"Rick Rothstein" wrote in message
...
As others have pointed out, you needed to use "String", not "string".
Whenever you do a logical test against a String constant (text within
quote marks), you must always be wary of case sensitivity. Some functions
(like InStr or Replace) have optional arguments whereby you can make the
comparison case insensitive; but others (like TypeName) do not.

--
Rick (MVP - Excel)


"Mike H" wrote in message
...
Hi,

Try this. Note a few less parenthesis

Sub Test()
'The active cell contains the string "ABC"
MsgBox ActiveCell.Value 'Displays 'ABC'
MsgBox TypeName(ActiveCell.Value) 'Displays 'String'
MsgBox TypeName(ActiveCell.Value) = TypeName("String") 'Displays
'FALSE'
End Sub

Mike

"ArthurJ" wrote:

Why does the last message box return FALSE when testing a cell for a
string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default TypeName function



"Peter T" wrote:

Try
MsgBox (LCase(TypeName(ActiveCell.Value)) = "string")

or of course simply change "string" to "String"

For this kind of test I would use

MsgBox VarType(ActiveCell.Value) = vbString

Regards,
Peter T

Thanks for the suggestion to use the VarType function. Unlike IsNumeric it identifies a string that looks like a number (ie, with a leading apostrophe). VarType is exactly what I needed.


Art
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default TypeName function

When comparing strings, I usually use StrComp rather than the "="
operator. With StrComp, you can specify whether the comparison is
case-sensitive, regardless of the Option Compare setting of the
module. E.g.,

If StrComp(VarName, "AbCd", vbTextCompare) = 0 Then
' strings match
Else
' string do not match
End If

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)


On Sun, 12 Oct 2008 15:36:00 +0100, "Peter T" <peter_t@discussions
wrote:

Along similar lines, to avoid case sensitivity with string comparisons could
head the module -

Option Compare Text

In general though best not to use that without specific reason.

Regards,
Peter T

"Rick Rothstein" wrote in message
...
As others have pointed out, you needed to use "String", not "string".
Whenever you do a logical test against a String constant (text within
quote marks), you must always be wary of case sensitivity. Some functions
(like InStr or Replace) have optional arguments whereby you can make the
comparison case insensitive; but others (like TypeName) do not.

--
Rick (MVP - Excel)


"Mike H" wrote in message
...
Hi,

Try this. Note a few less parenthesis

Sub Test()
'The active cell contains the string "ABC"
MsgBox ActiveCell.Value 'Displays 'ABC'
MsgBox TypeName(ActiveCell.Value) 'Displays 'String'
MsgBox TypeName(ActiveCell.Value) = TypeName("String") 'Displays
'FALSE'
End Sub

Mike

"ArthurJ" wrote:

Why does the last message box return FALSE when testing a cell for a
string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default TypeName function

The only thing I don't like about using StrComp is it uses 0 for equality.
Of course, I understand why... it makes perfect sense when you consider it
uses +1 for greater than and -1 for less than... but my mind always equates
0 with False in logical comparisons and so, when checking for string
equality, testing StrComp to 0 (a False type value) just seems so wrong to
me.

--
Rick (MVP - Excel)


"Chip Pearson" wrote in message
...
When comparing strings, I usually use StrComp rather than the "="
operator. With StrComp, you can specify whether the comparison is
case-sensitive, regardless of the Option Compare setting of the
module. E.g.,

If StrComp(VarName, "AbCd", vbTextCompare) = 0 Then
' strings match
Else
' string do not match
End If

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)


On Sun, 12 Oct 2008 15:36:00 +0100, "Peter T" <peter_t@discussions
wrote:

Along similar lines, to avoid case sensitivity with string comparisons
could
head the module -

Option Compare Text

In general though best not to use that without specific reason.

Regards,
Peter T

"Rick Rothstein" wrote in message
.. .
As others have pointed out, you needed to use "String", not "string".
Whenever you do a logical test against a String constant (text within
quote marks), you must always be wary of case sensitivity. Some
functions
(like InStr or Replace) have optional arguments whereby you can make the
comparison case insensitive; but others (like TypeName) do not.

--
Rick (MVP - Excel)


"Mike H" wrote in message
...
Hi,

Try this. Note a few less parenthesis

Sub Test()
'The active cell contains the string "ABC"
MsgBox ActiveCell.Value 'Displays 'ABC'
MsgBox TypeName(ActiveCell.Value) 'Displays 'String'
MsgBox TypeName(ActiveCell.Value) = TypeName("String") 'Displays
'FALSE'
End Sub

Mike

"ArthurJ" wrote:

Why does the last message box return FALSE when testing a cell for a
string?
I have the same problem checking for numeric values such as Double.

Sub Test()
'The active cell contains the string "ABC"
MsgBox (ActiveCell.Value) 'Displays 'ABC'
MsgBox (TypeName(ActiveCell.Value)) 'Displays 'String'
MsgBox (TypeName(ActiveCell.Value) = "string") 'Displays 'FALSE'
End Sub



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default TypeName function

The only thing I don't like about using StrComp is it uses 0 for equality.

No argument from me on that score.

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)

On Sun, 12 Oct 2008 11:54:00 -0400, "Rick Rothstein"
wrote:

The only thing I don't like about using StrComp is it uses 0 for equality.
Of course, I understand why... it makes perfect sense when you consider it
uses +1 for greater than and -1 for less than... but my mind always equates
0 with False in logical comparisons and so, when checking for string
equality, testing StrComp to 0 (a False type value) just seems so wrong to
me.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? SteveM Excel Programming 1 January 3rd 08 03:45 PM
TypeName of a UDT Pflugs Excel Programming 9 August 25th 07 09:29 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
I know selected picture is a shape but typename says is picture Carlos Lozano Excel Programming 3 July 24th 04 05:44 PM
Rephrasing argument for typename and a different result Hari[_3_] Excel Programming 6 June 15th 04 05:14 PM


All times are GMT +1. The time now is 05:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"