Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Tricky data type error

Was caught out by something tricky that I thought might be worth it to pass
on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there obviously will be
an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far as I can see
never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably at compile
time?


RBS



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Tricky data type error

Strangely (at least to me) this runs:

Sub test()

Dim strTest As String

strTest = 2

If strTest = 4 Then
MsgBox strTest
End If

End Sub

But this doesn't:


Sub test()

Dim strTest As String

strTest = "a"

If strTest = 4 Then
MsgBox strTest
End If

End Sub


This might in fact explain the different behaviour on the different
machines, rather than the Excel version.


RBS


"RB Smissaert" wrote in message
...
Was caught out by something tricky that I thought might be worth it to
pass on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there obviously will be
an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far as I can see
never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably at compile
time?


RBS




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Tricky data type error

I've never seen a situation in which that code wouldn't compile.
Of course, it blows up at run-time, but it should compile.

No, there's no way to catch such errors when you write or compile
the code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"RB Smissaert" wrote in message
...
Was caught out by something tricky that I thought might be
worth it to pass on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there
obviously will be an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far
as I can see never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably at
compile time?


RBS





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Tricky data type error

Well, I definitely have seen a compile error with this.
Thanks for confirming it can't be caught normally at compile time.

RBS

"Chip Pearson" wrote in message
...
I've never seen a situation in which that code wouldn't compile. Of
course, it blows up at run-time, but it should compile.

No, there's no way to catch such errors when you write or compile the
code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"RB Smissaert" wrote in message
...
Was caught out by something tricky that I thought might be worth it to
pass on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there obviously will
be an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far as I can
see never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably at compile
time?


RBS






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Tricky data type error

Do you remember what the Compiler Error was?


"RB Smissaert" wrote in message
...
Well, I definitely have seen a compile error with this.
Thanks for confirming it can't be caught normally at compile
time.

RBS

"Chip Pearson" wrote in message
...
I've never seen a situation in which that code wouldn't
compile. Of course, it blows up at run-time, but it should
compile.

No, there's no way to catch such errors when you write or
compile the code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"RB Smissaert" wrote in
message ...
Was caught out by something tricky that I thought might be
worth it to pass on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there
obviously will be an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far
as I can see never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably
at compile time?


RBS










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Tricky data type error

My mistake not to write it down, but I think it was similar
to the runtime error message.

RBS

"Chip Pearson" wrote in message
...
Do you remember what the Compiler Error was?


"RB Smissaert" wrote in message
...
Well, I definitely have seen a compile error with this.
Thanks for confirming it can't be caught normally at compile time.

RBS

"Chip Pearson" wrote in message
...
I've never seen a situation in which that code wouldn't compile. Of
course, it blows up at run-time, but it should compile.

No, there's no way to catch such errors when you write or compile the
code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"RB Smissaert" wrote in message
...
Was caught out by something tricky that I thought might be worth it to
pass on.

Had coded, simplified, this code:

Sub test()

Dim strTest As String

If strTest = 4 Then
MsgBox strTest
End If

End Sub

Strangely, this sometimes compiles and sometimes doesn't.
Unfortunately, it did with me, but it can't run as there obviously will
be an error:
Type mismatch (Error 13)
Strangely also it can sometimes run on Excel 2003, but as far as I can
see never on 2000.
Just completely unpredictable.

Would there be a way to catch out this coding error reliably at compile
time?


RBS









  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Tricky data type error


I think it has to do with certain assumption excel makes during runtime

If you try to place a non Compatable value in a variable. It will tr
to convert that variable into the type required and place the value i
it. However depending on what goes on with the variable beforehand i
may or May not be able to do this. When you declare a variable

Dim strtest as Strin

You DIMension up an area of memory in a "format" to receive strin
type. BUT strtest ISNT yet a string. This is decided when it gets i
first value IE strtest = 2 or StrTest = "a" Depending on what you d
from then on determines whether you get an error. It has something t
do with the fact that a String data type is really just a singl
dimension array of characters

While excel will try to work around your errors there are times when i
will just throw up its hands and go "Sod you Im Not Dealing with thi
Rubbish! YOu FIX it!!" IE you get a runtime erro

From memory.....It been awhil

--
Willo
-----------------------------------------------------------------------
Willot's Profile: http://www.excelforum.com/member.php...fo&userid=3516
View this thread: http://www.excelforum.com/showthread.php?threadid=54973

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
Error: Value in Formula of wrong data type mak_nyce Excel Worksheet Functions 0 January 5th 10 06:04 PM
Strange data type error johnsday Excel Programming 7 January 20th 06 04:08 PM
Dictionary object: Error assigning user defined data type to item Paul Urbanus Excel Programming 2 December 1st 05 04:21 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Type Mismatch Error when getting data from another workbook Tony Zappal Excel Programming 2 January 12th 05 10:29 PM


All times are GMT +1. The time now is 01:27 AM.

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"