Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default A disturbing behaviour of DIM (for variable declaration)


I have been wracking my brains on what happens when we declare a
variable, in spite of what programming literature says. The code below
declares X as integer and should accordingly handle all values within
the bounds -32763 to + 32673.
But the code generates an overflow error after p=30,000 and when x =3.

Sub Declaratn2()

Dim x As Integer
Dim p As Long
For x = 1 To 1000
p = x * 10000
MsgBox p
Next

End Sub

Is it correct to say that EXCEL is evaluating x against the product
x*1000? I am confused.

David.


--
davidm
------------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=484197

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default A disturbing behaviour of DIM (for variable declaration)

Hi David,

The reason being - when an integer is multiplied with another integer,
it will return an integer
So, 4 * 10000 (in your case) will make it 40000 & it will try to put
that result in a temporary integer variable (before assigning it to p
- which is long)

So, change your expression to p = clng(x) * 10000
Here, you are telling that multiply a long by an integer

Alternatively, you could write x * 10000.0 (this will make it a double
data type)

HTH
Kalpesh

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default A disturbing behaviour of DIM (for variable declaration)

Which is not that far off the answer Simon gave back in August <g.

Kalpesh wrote:
Hi David,

The reason being - when an integer is multiplied with another integer,
it will return an integer
So, 4 * 10000 (in your case) will make it 40000 & it will try to put
that result in a temporary integer variable (before assigning it to p
- which is long)

So, change your expression to p = clng(x) * 10000
Here, you are telling that multiply a long by an integer

Alternatively, you could write x * 10000.0 (this will make it a double
data type)

HTH
Kalpesh

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default A disturbing behaviour of DIM (for variable declaration)

And another reason never to use "As Integer"



davidm wrote:

I have been wracking my brains on what happens when we declare a
variable, in spite of what programming literature says. The code below
declares X as integer and should accordingly handle all values within
the bounds -32763 to + 32673.
But the code generates an overflow error after p=30,000 and when x =3.

Sub Declaratn2()

Dim x As Integer
Dim p As Long
For x = 1 To 1000
p = x * 10000
MsgBox p
Next

End Sub

Is it correct to say that EXCEL is evaluating x against the product
x*1000? I am confused.

David.

--
davidm
------------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=484197


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default A disturbing behaviour of DIM (for variable declaration)

you have a great memory, rowan<g

--


Gary


"Rowan Drummond" wrote in message
...
Which is not that far off the answer Simon gave back in August <g.

Kalpesh wrote:
Hi David,

The reason being - when an integer is multiplied with another integer,
it will return an integer
So, 4 * 10000 (in your case) will make it 40000 & it will try to put
that result in a temporary integer variable (before assigning it to p
- which is long)

So, change your expression to p = clng(x) * 10000
Here, you are telling that multiply a long by an integer

Alternatively, you could write x * 10000.0 (this will make it a double
data type)

HTH
Kalpesh



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
VBA - variable declaration Jeff Excel Discussion (Misc queries) 3 January 9th 08 12:45 PM
External Variable Declaration ehntd[_3_] Excel Programming 1 October 29th 04 11:57 AM
External Variable Declaration ehntd[_2_] Excel Programming 1 October 29th 04 11:36 AM
Global variable declaration! aiyer[_12_] Excel Programming 3 April 13th 04 04:35 PM
Variable Declaration?? Tom Ogilvy Excel Programming 1 August 8th 03 06:45 PM


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