Thread: Variable types
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Variable types

Two points. First, I would probably Dim the 'num' variable As Double since
it is being assigned back as the value to a cell and we don't know for sure
that it is always going to be a whole number. Second, I would always use As
Long for variables that only take integer values. On 32-bit operating
systems, numbers Dim'med As Integer will be stored in the same memory space
as those Dim'med As Long (4 bytes = 32 bits); so, by making them As Long to
begin with, you save the operating system from having to maintain whatever
it is it maintains to track Integers (within the 4-byte memory storage it
ends up in)... so, in the long run, As Long is more efficient than As
Integer.

--
Rick (MVP - Excel)


"Gary''s Student" wrote in message
...
Option Explicit
Sub FillRange2()
Dim num As Integer, row As Integer, col As Integer
num = 1
For row = 0 To 9
For col = 0 To 9
Sheets("Sheet1").Range("A1").Offset(row, col).Value = num
num = num + 1
Next col
Next row
End Sub

Here integer is o.k. because the values are small.
--
Gary''s Student - gsnu200842


"Diddy" wrote:

Hi,

I'm a bit of a dabbler with VBA, I use a combination of recorded code,
snippets from the web and user groups and (thankfully) code supplied in
answers here.
At the moment I'm trying to learn a bit about referring to ranges as I'm
hoping to use Offset in some work. The site I'm looking at is
http://spreadsheetpage.com/index.php...your_vba_code/

I'm totally confused about variables and DIM statements but understand
the
reasons for using Option Explicit (I hope).

So my problem is that I've copied a bit of code from the website and
don't
know how to dim the variables. I could spend all day trying various
different
things but I was hoping that if I came here I could get some help and
maybe
someone could explain the why of it too.

Here's the code

Sub FillRange2()
Num = 1
For Row = 0 To 9
For Col = 0 To 9
Sheets("Sheet1").Range("A1").Offset(Row,Col).Value = Num
Num = Num + 1
Next Col
Next Row
End Sub

Many thanks