Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cell Comparsion (a bug?)

Hi all,

I am wondering if this is a bug for excel.

Here is the (simplified and ugly) code: (Problem details right afte
the code)


Sub test()
Dim i As Integer, j As Integer, k As Integer, h As Integer, l A
Integer
Dim poNo As String

k = 5

For h = 1 To Worksheets("Raw Data").UsedRange.Columns.Count
If Cells(1, h).Value = "Po No" Then
Exit For
End If
Next h

For j = 1 To Worksheets("Raw Data").UsedRange.Columns.Count
If Cells(1, j).Value = "Je Period" Then
Exit For
End If
Next j
End Sub

I have 2 sheets (Settings, Raw Data)
When I add the following line just before "End Sub" and run:
Msgbox _
Worksheets("Settings").Cells(66,h).Value = _
Worksheets("Raw Data").Cells(149, j).Value

Msgbox with "False" is appeared.

Later on, I changed the line to:
Msgbox _
Worksheets("Settings").Cells(66,h).Value & " " &
Worksheets("Raw Data").Cells(149, j).Value

Msgbox with "123456 123456" is appeared.

And further I changed the line to :
Msgbox _
Len(Worksheets("Settings").Cells(66,h).Value) & " " &
Len(Worksheets("Raw Data").Cells(149, j).Value)

And

Msgbox Instr(1, _
Worksheets("Settings").Cells(66,h).Value, _
Worksheets("Raw Data").Cells(149, j).Value)

Result of former one: 6 6
Result of latter one: 1

So I am wondering why direct cell comparsion will give "false".

Thanks

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 733
Default Cell Comparsion (a bug?)

"kaon " wrote...
....
I have 2 sheets (Settings, Raw Data)
When I add the following line just before "End Sub" and run:
Msgbox _
Worksheets("Settings").Cells(66,h).Value = _
Worksheets("Raw Data").Cells(149, j).Value

Msgbox with "False" is appeared.

Later on, I changed the line to:
Msgbox _
Worksheets("Settings").Cells(66,h).Value & " " &
Worksheets("Raw Data").Cells(149, j).Value

Msgbox with "123456 123456" is appeared.

....

What does the statement

MsgBox _
Application.WorksheetFunctions.IsNumber( _
Worksheets("Settings").Cells(66, h).Value) & " " _
& Application.WorksheetFunctions.IsNumber( _
Worksheets("Raw Data").Cells(149, j).Value)

return?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cell Comparsion (a bug?)

em.. I think this would give me "True True" coz I entered the numbe
manually. (just press 123456)

Interestingly, when I compare either one of them to a variable, say,

Dim PoNo as String

PoNo = Worksheets("Settings").Cells(66, h).Value
If Worksheets("Raw Data").Cells(149, j).Value = PoNo Then
'Blah blah blah
End if

It will run the statement inside the if-block!
but I cannot do DIRECT comparsion using =

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 733
Default Cell Comparsion (a bug?)

"kaon " wrote...
em.. I think this would give me "True True" coz I entered the number
manually. (just press 123456)

....

OK, don't eliminate this as a possibility. But before you dismiss a very
likely explanation for why your code is fubar, consider that all the other
tests you've tried except for the equality comparison induce implicit number
to text conversion. Once both cells' values are converted to text, of course
they'll give the results you've already stated, but if one of them has
numeric type and the other string type, equality comparison *will* result in
FALSE. Just because you entered two 'numbers' manually doesn't mean one of
the two cells may not have number format Text.

Basic debugging concepts: assume nothing, and never trust the validity of
any inputs, especially not your own.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 837
Default Cell Comparsion (a bug?)

Manually entering the number does not preclude the possibility that the
cell was preformatted as text (which can be induced implicitly by a cell
formula). As Harlan has pointed out, all of your posted results are
consistent with one value being 123456 (number) and the other one being
"123456" (string).

Minor correction to Harlan's suggestion: there is no "s" on the end of
Application.WorksheetFunction

Jerry

kaon < wrote:

em.. I think this would give me "True True" coz I entered the number
manually. (just press 123456)

Interestingly, when I compare either one of them to a variable, say,

Dim PoNo as String

PoNo = Worksheets("Settings").Cells(66, h).Value
If Worksheets("Raw Data").Cells(149, j).Value = PoNo Then
'Blah blah blah
End if

It will run the statement inside the if-block!
but I cannot do DIRECT comparsion using =!




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
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Options Yuvraj Excel Discussion (Misc queries) 0 June 29th 09 11:20 AM
Populate a cell if values in cell 1 and cell 2 match cell 3 and 4 [email protected] Excel Worksheet Functions 1 August 22nd 08 02:04 AM
VLookup Columns Comparsion CuriousMe Excel Worksheet Functions 2 December 21st 06 12:31 AM
Help on and IF comparsion JustMe602 Excel Discussion (Misc queries) 1 May 19th 06 12:38 AM
How to create/run "cell A equals Cell B put Cell C info in Cell D abmb161 Excel Discussion (Misc queries) 5 January 26th 06 06:36 PM


All times are GMT +1. The time now is 08:16 PM.

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

About Us

"It's about Microsoft Excel"