Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / October 2008

Tip: Looking for answers? Try searching our database.

TypeName function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ArthurJ - 12 Oct 2008 14:46 GMT
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
Peter T - 12 Oct 2008 15:01 GMT
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

> Why does the last message box return FALSE when testing a cell for a
> string?
[quoted text clipped - 6 lines]
>    MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
> End Sub
ArthurJ - 12 Oct 2008 15:46 GMT
> Try
>     MsgBox (LCase(TypeName(ActiveCell.Value)) = "string")
[quoted text clipped - 9 lines]
>
> 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
Mike H - 12 Oct 2008 15:02 GMT
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

> 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.
[quoted text clipped - 5 lines]
>     MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
> End Sub
Rick Rothstein - 12 Oct 2008 15:18 GMT
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.

Signature

Rick (MVP - Excel)

> Hi,
>
[quoted text clipped - 20 lines]
>>     MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
>> End Sub
Peter T - 12 Oct 2008 15:36 GMT
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

> As others have pointed out, you needed to use "String", not "string".
> Whenever you do a logical test against a String constant (text within
[quoted text clipped - 26 lines]
>>>     MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
>>> End Sub
Chip Pearson - 12 Oct 2008 16:29 GMT
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)

>Along similar lines, to avoid case sensitivity with string comparisons could
>head the module -
[quoted text clipped - 36 lines]
>>>>     MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
>>>> End Sub
Rick Rothstein - 12 Oct 2008 16:54 GMT
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.

Signature

Rick (MVP - Excel)

> When comparing strings, I usually use StrComp rather than the "="
> operator. With StrComp, you can specify whether the comparison is
[quoted text clipped - 58 lines]
>>>>>     MsgBox (TypeName(ActiveCell.Value) = "string")  'Displays 'FALSE'
>>>>> End Sub
Chip Pearson - 12 Oct 2008 16:57 GMT
>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)

>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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.