×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to define an Array?

How to define an Array?

How to define an Array?

(OP)
Hi,

I would like to know how can I define an Array in VB (Excel)?

I tried the following commands:

Dim myArray(5) As Integer

myArray = Array(12,3,13,64,245,75)

But, I got a compile error as follows:

               Can't assign to array

Please let me know what is the proper way for defining an Array?

Thanks,
A.A.Y.
 

RE: How to define an Array?

The two statements don't work together.

You can either use:

CODE

myArray = Array(12, 3, 13, 64, 245, 75)
' (no dim statement)
' won't work if option explicit is included

Or else you can use:

CODE

dim myArray(5) As Integer
myArray(0) = 12
myArray(1) = 3
myArray(2) = 13
myArray(3) = 64
myArray(4) = 245
myArray(5) = 75

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: How to define an Array?

Or dimension the array as a variant (which is the default if you don't dimension it).  Then you can leave the Option Explicit statement in place.

You can also transfer the contents of a worksheet range to an array:

CODE

Dim MyArray as Variant

MyArray = Range("MyRange").Value2

Note that:
1. The array is automatically re-sized to the size of the range, so you don't need to specify the array size (in fact it won't work if you do).

2. Use .Value2 rather than .Value (the default) or nothing because it is quicker and it won't truncate values formatted as currency to 4 decimal places.

3.  All arrays will be 2D, base 1, so a range of 1 row x 10 columns will come in as MyArray(1 to 1, 1 to 10).

Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
 

RE: How to define an Array?

Quote (ids):

Or dimension the array as a variant (which is the default if you don't dimension it).  Then you can leave the Option Explicit statement in place.
No, that doesn't work for me.  The following code gives an error ("Compile Error: Can't Assign to Array"):

CODE

Dim myArray(5) As Variant
myArray = Array(12, 3, 13, 64, 245, 75)
 

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

RE: How to define an Array?

Pete - you didn't read my notes!

Either
Dim MyArray as variant
or
Dim MyArray() as variant

will work.


The array is automatically redimmed when you assign something to it.

Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
 

RE: How to define an Array?

(OP)
Hi,

Thank you for your replies. They helped me to solve my bug.

A.A.Y.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close