Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


Can someone help clear this for me?
The declaration of the variable x as Integer works perfectly in:

Sub Declaratn()
Dim x as Integer

For x = 1 to 1000
Cells(x,1) = x
Next

End sub


...but produces overflow error at x=2 in:

Sub Declaratn2()

Dim x as Integer

For x = 1 to 1000
Cells(x,1) = x*30000
Next

End sub

My hunch is that Excel seems to evaluate x*30000 and allocates memor
space to the product value. And since "integer" has an upper limit o
32767, the code crashes at x=2 (which produces 60,000). But shouldn'
Excel be concerned with allocating memory space to x over the rang
1-1000 in this instance? I would welcome an explanation.

TI

--
david
-----------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...fo&userid=2064
View this thread: http://www.excelforum.com/showthread.php?threadid=39800

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


David
I think you are right
As the formula is multiplying 2 ints VB assumes the result will be an
int.
Fixes include:
Cells(x, 1).Value = x * CLng(30000)
Cells(x, 1).Value = x * (38000 - 8000)
Cells(x, 1).Value = clng(x) * 30000
etc

Basically it looks like you need to warn VB that this will overflow, or
type x as a long.

cheers
Simon


--
Simon Murphy
------------------------------------------------------------------------
Simon Murphy's Profile: http://www.excelforum.com/member.php...o&userid=26538
View this thread: http://www.excelforum.com/showthread...hreadid=398008

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


Simon,

The thrust of my question is that if a variable is declared (as intege
etc.), shouldn't its character be* solely* governed by the value
assigned to it-1 to 1000 in the example? I had never thought o
declaration affecting the calculations the variable is made t
undertake, but that seems to be case. Strange

--
david
-----------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...fo&userid=2064
View this thread: http://www.excelforum.com/showthread.php?threadid=39800

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


David
sorry I should have been clearer
x is not overflowing, the result of your formula is overflowing. The
issues isn't so much what x can and can't do, but how VB guesses the
data type of a formula result. (hence why 38000-8000 fixes it)
effectively you are taking a short cut from this:

result = x*30000
Cells(x, 1).Value = result

The question is what is the type of result?
answer: most programming languages assume the largest data type of any
of the arguments, which is why changing one to a long fixes it.

If you replace your short version with the 2 lines above and dim result
as a long then the problem is still there. VB internally apparently
can't calculate x*30000 where x is an int 1.

So your observation is right - the data type does have a big impact on
what can be done with a variable, and part of that is how VB guesses
the data type of a formula result, based on the data type of all the
arguments

I rarely use ints anyway in VBA, I mainly use longs.

cheers
Simon


--
Simon Murphy
------------------------------------------------------------------------
Simon Murphy's Profile: http://www.excelforum.com/member.php...o&userid=26538
View this thread: http://www.excelforum.com/showthread...hreadid=398008

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


Simon,

Thanks for your elaborate explanation. I however cannot still get m
head round "what a declaration does to a variable" in the light of th
quirky behaviour we are analysing. In the sub

Sub OverflowError()

Dim x as integer
Dim result as long

For i = 1 to 10
result = x*30000
next

End sub

as you correctly pointed out, VBA cannot get past x=2, only becaus
*"VB internally apparently can't calculate x*30000 where x is an in
1". * Given this,

I will driven to wonder when a variable x can be declared as integeri
order to benefit from optimal memory location. In other words, when ca
one profitably go INTEGER and avoid the profligacy of going LONG o
DOUBLE.

I know the problem in the above code (as in the original) can be fixe
by avoiding INTEGER declaration (and using the default variant) o
declaring LONG (or DOUBLE if you will) but these are options we migh
not want to take if the goal of programming speed and efficiency i
pursued. The whole exercise seems a bit academic: nonetheless, I a
looking at the greater picture of what can be done to fully harness th
advantages of declarating data types appropriately.

David

--
david
-----------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...fo&userid=2064
View this thread: http://www.excelforum.com/showthread.php?threadid=39800



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Declaration problem


David
Longs are the native size and therefore more effecient than ints on 32
bit machines. Well (slightly) faster anyway, memory is the same I
think.

You can keep it as an int if you do the 38000-8000 shuffle (which lets
vb know one of the args is a long). This therefore does not affect
your declaration of x.

You are right, it is odd, I'm sure I read about this a long time ago,
no idea where though. It looks like the issue is still there in .net
if thats any consolation.

cheers
Simon


--
Simon Murphy
------------------------------------------------------------------------
Simon Murphy's Profile: http://www.excelforum.com/member.php...o&userid=26538
View this thread: http://www.excelforum.com/showthread...hreadid=398008

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 Declaration problem Shawn Excel Programming 2 July 17th 05 10:44 PM
Declaration name Robert Hargreaves[_2_] Excel Programming 4 June 6th 05 04:48 PM
duplicate declaration [email protected] Excel Programming 1 March 1st 05 02:03 AM
which declaration to use Peer Excel Programming 3 August 2nd 04 03:17 PM
Declaration? TJF[_2_] Excel Programming 5 December 18th 03 03:26 PM


All times are GMT +1. The time now is 10:22 AM.

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"