Excel 2007 Need help for test

  • Thread starter Thread starter danya_emfinger
  • Start date Start date
D

danya_emfinger

I hope somebody can please help asap. I have a huge computer
programming exam tomorrow and I do not understand how to use the redim
statement with dynamic arrays or how to call a subroutine. I have to
know how to do both by tomorrow. Could sombody please break it down
to me. I have been reading excel 2007 vba for dummies but it kind of
skips over these two areas.
 
When you declare a dynamic array, use the following syntax:

Dim myArray() as Integer

At that point, the size is not determined. You use the ReDim
statement as follows:

ReDim myArray(20)

This will create an array of 20 elements. The first element is
referred to as follows:

myArray(lBound(myArray)) = 1

The last element is referred to as follows:

myArray(uBound(myArray)) = 2

If you ReDim the array again, all the data in it will be overwritten.
To leave the data, you use the Preserve keyword:

ReDim Preserve myArray(50)

Doing so will leave intact the first 20 items (if you have populated
them), and create 30 new ones.


Here's a subroutine example. I'll call the subroutine mySub:

Public Sub mySub()
' do stuff here
End Sub

Now, let's say your main routine is called MainRoutine, which calls
mySub:

Public Sub MainRoutine()
Call mySub()
End Sub
 
Unless you do something special:
ReDim myArray(20)
will result in an array with 21 elements.

It'll be the same as:
ReDim myArray(0 to 20)

I find that always specifying the upper and lower bounds makes the code more
self-documenting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top