Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default Rephrasing argument for typename and a different result

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Rephrasing argument for typename and a different result

Hari,

Did you compile the code and not get any warnings? Looks like the following
line has one too many left parentheses.
If TypeName((Sheets(sheetName).Cells(n, 2)) < "Date" Then

How about:
If TypeName(Sheets(sheetName).Cells(n, 2)) < "Date" Then

Troy

"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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default Rephrasing argument for typename and a different result

Hi Troy,

Im sorry, I added a parentheses by mistake when posting the message to the
group.

The original problem still persists.

Regards,
Hari
India



"TroyW" wrote in message
...
Hari,

Did you compile the code and not get any warnings? Looks like the

following
line has one too many left parentheses.
If TypeName((Sheets(sheetName).Cells(n, 2)) < "Date" Then

How about:
If TypeName(Sheets(sheetName).Cells(n, 2)) < "Date" Then

Troy

"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






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Rephrasing argument for typename and a different result

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




  #5   Report Post  
Posted to microsoft.public.excel.programming
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








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Rephrasing argument for typename and a different result

Hari,

The TypeName function is used to inspect objects and variables and return
the data type. Take a look at the VBA Help.

CellValue == TypeName
hello == String
1234 == Double
01/01/2004 == Date
(blank) == Empty


In your snippet of code, it appears that the INTENT of the code logic is to
inspect the CONTENTS of the cell(n,2) and determine what type of VALUE is
in the cell. But, the way your code is currently written, the TypeName
function can only return a value of "Range" no matter what is entered into
the cell (text, numbers, dates, etc.). This is not a syntax error which
results in the program stopping, but it is a logic error that won't return
the desired result.

In order to inspect the contents of the cell correctly you must write the
code as follows (Note the addition of ".Value" to the end of the statement):

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

-or-

If TypeName(Sheets(sheetName).Cells(n,2).Value

Please change the code to what I'm suggesting and tell me if you still have
errors. Or, help me to understand what the intent or purpose of the code is.

Troy

P.S. Excel has various object types (workbook, worksheet, range, chart,
etc.). You can use TypeName to inspect those objects also and return what
type they are. Your current code is asking the question what object type is
a cell. In Excel, a cell has a type "Range". A "cell" and a "cell value" are
two different things.


"Hari" wrote in message
...
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








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default Rephrasing argument for typename and a different result

Hi Troy,

Thanx a ton.

I am able to get what I wanted with ur method. I have used ".value" at the
end.

Thanx again

Regards,
Hari
India


"TroyW" wrote in message
...
Hari,

The TypeName function is used to inspect objects and variables and return
the data type. Take a look at the VBA Help.

CellValue == TypeName
hello == String
1234 == Double
01/01/2004 == Date
(blank) == Empty


In your snippet of code, it appears that the INTENT of the code logic is

to
inspect the CONTENTS of the cell(n,2) and determine what type of VALUE

is
in the cell. But, the way your code is currently written, the TypeName
function can only return a value of "Range" no matter what is entered into
the cell (text, numbers, dates, etc.). This is not a syntax error which
results in the program stopping, but it is a logic error that won't return
the desired result.

In order to inspect the contents of the cell correctly you must write the
code as follows (Note the addition of ".Value" to the end of the

statement):

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

-or-

If TypeName(Sheets(sheetName).Cells(n,2).Value

Please change the code to what I'm suggesting and tell me if you still

have
errors. Or, help me to understand what the intent or purpose of the code

is.

Troy

P.S. Excel has various object types (workbook, worksheet, range, chart,
etc.). You can use TypeName to inspect those objects also and return what
type they are. Your current code is asking the question what object type

is
a cell. In Excel, a cell has a type "Range". A "cell" and a "cell value"

are
two different things.


"Hari" wrote in message
...
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










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
Rephrasing Interior.ColorIndex 36 question. Linda New Users to Excel 1 May 28th 10 01:49 PM
Function argument correct result but answer 0 in cell? Tonzie Excel Worksheet Functions 3 March 17th 08 01:06 PM
Advanced formula - Return result & Show Cell Reference of result Irv Excel Worksheet Functions 7 May 6th 06 03:36 AM
vlookup based on random result returns incorrect result rickat Excel Worksheet Functions 1 December 6th 05 01:16 PM
dynamic arrays as an argument/result in programmed functions AjaxRocks Excel Programming 2 November 24th 03 09:56 PM


All times are GMT +1. The time now is 07:06 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"