Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA code optimization - why using long instead of integer?

Can anyone explain to me that why VBA converts all integer values to long to
make the program run faster? Mean that Integer is meaningless?

"VBA converts all integer values to type Long, even if they are declared as
type Integer. In fact, Long variables might be slightly faster because VBA
does not have to convert them."
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default VBA code optimization - why using long instead of integer?

Short answer, in 32-bit systems 2 byte integers are converted to 4 byte
Longs. There really is no other way so that respective bits correctly line
up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

Obviously -1 does not equal 65535 yet the computer is returning the correct
answer, namely
"FFFF" = "FFFF"

However had we coerced the -1 to a long first we would have got the right
answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False
"FFFFFFFF" = "FFFF"

Generally there is no point in VBA to declare "As Integer" in modern
systems, except perhaps for some legacy API's that expect to receive an
Integer.

Regards,
Peter T



"Thao" wrote in message
...
Can anyone explain to me that why VBA converts all integer values to long
to
make the program run faster? Mean that Integer is meaningless?

"VBA converts all integer values to type Long, even if they are declared
as
type Integer. In fact, Long variables might be slightly faster because VBA
does not have to convert them."



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA code optimization - why using long instead of integer?

But if the compiler converts integer variable to long, it means that
declaration variables as integer or long is not important anymore since at
run-time, all of them are long.
Eg: 2 same VBA codes, one is using Integer, the other is using Long. At
run-time,using the same computer will these two codes finish running at the
same time?

Why do they still recommend that we should use Long when writing the VBA
code instead of Long since both will be Long after compiling?

Thanks and best regards,
Thao Vo

"Peter T" wrote:

Short answer, in 32-bit systems 2 byte integers are converted to 4 byte
Longs. There really is no other way so that respective bits correctly line
up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

Obviously -1 does not equal 65535 yet the computer is returning the correct
answer, namely
"FFFF" = "FFFF"

However had we coerced the -1 to a long first we would have got the right
answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False
"FFFFFFFF" = "FFFF"

Generally there is no point in VBA to declare "As Integer" in modern
systems, except perhaps for some legacy API's that expect to receive an
Integer.

Regards,
Peter T



"Thao" wrote in message
...
Can anyone explain to me that why VBA converts all integer values to long
to
make the program run faster? Mean that Integer is meaningless?

"VBA converts all integer values to type Long, even if they are declared
as
type Integer. In fact, Long variables might be slightly faster because VBA
does not have to convert them."




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default VBA code optimization - why using long instead of integer?

AFAIK the compiler does not convert a number from one data type to another.
However an Integer gets converted to a long at the moment it is used, in
effect an extra process.

In practise I don't suppose you'd notice the difference between calculating
with Integers vs Longs (assuming numbers within 32k). However it does not
serve any useful purpose to declare Integer variables.

A common catch is when an Integer variable is used to refer to a cell row,
then wonder why the code fails when it tries to refer to any row over 32767

Regards,
Peter T



"Thao" wrote in message
...
But if the compiler converts integer variable to long, it means that
declaration variables as integer or long is not important anymore since at
run-time, all of them are long.
Eg: 2 same VBA codes, one is using Integer, the other is using Long. At
run-time,using the same computer will these two codes finish running at
the
same time?

Why do they still recommend that we should use Long when writing the VBA
code instead of Long since both will be Long after compiling?

Thanks and best regards,
Thao Vo

"Peter T" wrote:

Short answer, in 32-bit systems 2 byte integers are converted to 4 byte
Longs. There really is no other way so that respective bits correctly
line
up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

Obviously -1 does not equal 65535 yet the computer is returning the
correct
answer, namely
"FFFF" = "FFFF"

However had we coerced the -1 to a long first we would have got the right
answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False
"FFFFFFFF" = "FFFF"

Generally there is no point in VBA to declare "As Integer" in modern
systems, except perhaps for some legacy API's that expect to receive an
Integer.

Regards,
Peter T



"Thao" wrote in message
...
Can anyone explain to me that why VBA converts all integer values to
long
to
make the program run faster? Mean that Integer is meaningless?

"VBA converts all integer values to type Long, even if they are
declared
as
type Integer. In fact, Long variables might be slightly faster because
VBA
does not have to convert them."






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default VBA code optimization - why using long instead of integer?

Although behind the scenes the 16-bit value is converted to a 32 bit for
processing (an Integer to a Long) that has nothing to do with the data type
of the named variable (or return type of say a function) that "points" to
the value in memory. Also the value remains in memory as its original data
type. So you still need to be careful with not only data types of variables
but even the data types of simple numbers in some cases, to illustrate

dim n as Long
n = 32767 + 1 ' overflow

It fails because of the attempt to create 32768 as an integer, even though
"n" is declared as a Long. To fix need to convert one of those numbers to a
Long before doing the addition, by appending with a & or with the cLng
function.

Regards,
Peter T





Regards,
Peter T


"JLGWhiz" wrote in message
...
If the integer is automatically converted to a long, why do we still get
the
overflow message when we use itegers and try to execute a procedure that
exceeds the integer capacity?

"Peter T" wrote:

AFAIK the compiler does not convert a number from one data type to
another.
However an Integer gets converted to a long at the moment it is used, in
effect an extra process.

In practise I don't suppose you'd notice the difference between
calculating
with Integers vs Longs (assuming numbers within 32k). However it does not
serve any useful purpose to declare Integer variables.

A common catch is when an Integer variable is used to refer to a cell
row,
then wonder why the code fails when it tries to refer to any row over
32767

Regards,
Peter T



"Thao" wrote in message
...
But if the compiler converts integer variable to long, it means that
declaration variables as integer or long is not important anymore since
at
run-time, all of them are long.
Eg: 2 same VBA codes, one is using Integer, the other is using Long. At
run-time,using the same computer will these two codes finish running at
the
same time?

Why do they still recommend that we should use Long when writing the
VBA
code instead of Long since both will be Long after compiling?

Thanks and best regards,
Thao Vo

"Peter T" wrote:

Short answer, in 32-bit systems 2 byte integers are converted to 4
byte
Longs. There really is no other way so that respective bits correctly
line
up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

Obviously -1 does not equal 65535 yet the computer is returning the
correct
answer, namely
"FFFF" = "FFFF"

However had we coerced the -1 to a long first we would have got the
right
answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False
"FFFFFFFF" = "FFFF"

Generally there is no point in VBA to declare "As Integer" in modern
systems, except perhaps for some legacy API's that expect to receive
an
Integer.

Regards,
Peter T



"Thao" wrote in message
...
Can anyone explain to me that why VBA converts all integer values to
long
to
make the program run faster? Mean that Integer is meaningless?

"VBA converts all integer values to type Long, even if they are
declared
as
type Integer. In fact, Long variables might be slightly faster
because
VBA
does not have to convert them."








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
Excel code optimization deepika :excel help[_2_] Excel Discussion (Misc queries) 5 January 29th 08 01:02 PM
VBA Code Optimization [email protected] Excel Programming 3 December 12th 07 04:03 AM
Declaring variables (Long, String, Integer) and interpretation spe MrT Excel Programming 4 December 12th 04 12:43 PM
Hash a range, output a Long Integer? Stephen Rasey[_2_] Excel Programming 2 June 12th 04 04:59 AM
calculate/concert long/integer to date time Yejeet Excel Programming 2 October 7th 03 10:39 PM


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