View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Proper Programming


"COM" wrote


until you get to 32,767, the next increment will flip the
variable back to it's lower limit. So in this case (for an
Integer) 32,767 + 1 = -32,768.
<<<

That is false. Incrementing past the upper limit does NOT flip
the value to the lower limit. It causes an error 6: overflow.


I don't remember the command, but it's possible to set the range
of Integer instead from -32,768 to 32,767, it can be set to 0 to
65535.
<<<

This, too, is incorrect. Integers and Longs are always signed.
There is no way to use unsigned variables in VBA.


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


"COM" wrote in message
...
Something you pointed out that has not yet been explained..

(Probably other things too that could help, but I'm going to
cover this one item.)

You said you didn't know what it all means.

As everyone else has said,

Dim thisThing

Provides memory for the variable thisThing as a Variant, by

default. However, if you wish to declare your variables as a
type, you should use the following as a guide for your variable
selection. Remember, that in programming, you should always
preplan your job, so that you can try to solve any problems as
soon as possible, and not have to redo all the thousands of lines
of code you may end up writing. (not hard to get to thousands of
lines, if you try to deal with the multitude of potential
problems that you identify, and all the things to keep/guide the
user to correct and controled data entry.)

For example, if you know that you are only going to be using

yes/no, true/false, on/off, etc. type data, then declaring the
variable as a boolean by the following line, will give you a
variable that is just that, either true or false.

Dim IsTrue as Boolean

So you could write something like.

if IsTrue then
msgbox("The result is true.")
else
msgbox("The result is false.")
end if

Something that helps you, and aids in Excel not crashing, is to

use the Option Explicit command at the top of your code, outside
of a sub routine or function.

Like

Option Explicit

Public Sub TrueText()
Dim IsTrue as Boolean

IsTrue = True

If IsTrue then
MsgBox("Is True")
Else
MsgBox("Is False")
End If

End Sub

By using the Option Explicit, the debugger will require that

each variable that you try to use, must first be declared. Now
you don't ***have*** to declare it as a particular type, but as
has been discussed, in the long run if you know what type of data
it will be then, it realllllllllllly can help.

What I have found, is that if you have extensive code, and you

do not declare each variable, VBA will take care of the
"assignments" as necessary, but at some point it runs out of
memory, or runs into an error, and the code crashes. Obviously
if you haven't saved your work, then everything changed since
your last save is blasted away.

Anyways, here is the list of data types available, and their

restrictions (Range). For an example, if you declare a variable
as an Integer, and say you increment the variable by one until
you get to 32,767, the next increment will flip the variable back
to it's lower limit. So in this case (for an Integer) 32,767 + 1
= -32,768. It can be discouraging, but that's how the variables
work. It's all based on the binary representation of the
numbers.

I don't remember the command, but it's possible to set the

range of Integer instead from -32,768 to 32,767, it can be set to
0 to 65535.

The following information was taken from the help for 'Data

Type Summary'
Data type Storage size Range
Byte 1 byte 0

to 255
Boolean 2 bytes

True or False
Integer 2

-32,768 to 32,767
Long
(long integer) 4

-2,147,483,648 to 2,147,483,647
Single
(single-precision floating-point) 4

-3.402823E38 to -1.401298E-45 for negative values;
1.401298E-45

to 3.402823E38 for positive values
Double
(double-precision floating-point) 8

-1.79769313486232E308 to

-4.94065645841247E-324 for negative values;

4.94065645841247E-324 to

1.79769313486232E308 for positive values
Currency
(scaled integer) 8

-922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 14 bytes

+/-79,228,162,514,264,337,593,543,950,335 with no decimal point;

+/-7.9228162514264337593543950335 with 28 places to the right of

the decimal;

smallest non-zero number is +/-0.0000000000000000000000000001
Date 8 bytes

January 1, 100 to December 31, 9999
Object 4 bytes

Any Object reference
String
(variable-length) 10 bytes + string length 0 to

approximately 2 billion
String
(fixed-length) Length of string

1 to approximately 65,400
Variant
(with numbers) 16 bytes

Any numeric value up to the range of a Double
Variant
(with characters) 22 bytes + string length Same

range as for variable-length String
User-defined
(using Type) Number required by elements The

range of each element is the same as the range of

its data type.