View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Hari[_3_] Hari[_3_] is offline
external usenet poster
 
Posts: 157
Default Rephrasing argument for typename and a different result

Hi Troy,

This is strange for me.

What datatype is range ( Im a newbie...)

Also, my original question remains. Why does excel be able to work correctly
when we write

dates = Sheets(sheetName).Cells(n, 2)
If TypeName(dates) < "Date" Then


But if the statement is written as
If TypeName((Sheets(sheetName).Cells(n, 2)) < "Date" Then

the result after running is different in the above 2 cases as originally
stated

Regards,
Hari
India


"TroyW" wrote in message
...
Hari,

Looking closer, I don't think either statement is correct. My testing
indicates that you need to qualify that you want the "Value" of the cell,
otherwise TypeName will always return a "Range" type. Try testing

different
values of the cell with the code below.

sType1 changes depending upon what value is in the cell. sType2 is always
"Range", independent of what the cell value is.

I think you want to use:
TypeName(Sheets(sheetName).Cells(n, 2).Value)

Troy


Sub TypeName_Example()
Dim sType1 As String
Dim sType2 As String

'''n and sheetName need to be defined.

sType1 = TypeName(Sheets(sheetName).Cells(n, 2).Value)
sType2 = TypeName(Sheets(sheetName).Cells(n, 2))
MsgBox "Type1 = " & sType1 & vbCr & _
"Type2 = " & sType2

End Sub


"Hari" wrote in message
...
Hi,


I have the following code which executes perfectly.

dates = Sheets(sheetName).Cells(n, 2)
If TypeName(dates) < "Date" Then
Sheets(sheetName).Cells(n, 2).Interior.ColorIndex = 6
NoOfErrors = NoOfErrors + 1
Else



If I rewrite the above code as given below and run it ( without changing

any
data in the worksheet) then it highlights an error in the cell which has
date only. How is the 2 codes different from each other?


If TypeName((Sheets(sheetName).Cells(n, 2)) < "Date" Then
Sheets(sheetName).Cells(n, 2).Interior.ColorIndex = 6
NoOfErrors = NoOfErrors + 1
Else

Regards,
Hari
India