Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficulty assigning values to array elements

Status
Not open for further replies.

huggybear

Technical User
Feb 15, 2001
83
0
0
US
Please help me. I am having difficulty assigning values to array elements.

When I use this method:

Dim y
y = Array(7,6,5,4,etc)

I get an error when I try to compile, "Invalid outside procedure"

I get the same error when I use this method:

Option Base = 1

Dim y(4) As Integer
y(1) = 1

I don't get it. Do you?

Thanks
 
Are you trying to use the array function outside of a procedure? You can not, just like any other function.

Public Sub test()
Dim y() As Variant
y = Array(1, 2, 3, 4, 5)
Debug.Print y(4)

Dim z(1 To 4) As Integer
z(1) = 123
Debug.Print z(1)
End Sub

output
5
123
 
Error said:
Invalid outside procedure

You can declare the variable outside a procedure, but you can't set it. Here is an example:
Code:
Option Explicit

[green]'Global declaration[/green]
Dim y

Sub SetGlobalArray()
y = Array(7,6,5,4)
End Sub

Sub SetLocalArray()
Dim z(4) As Integer
z(1) = 1
End Sub
Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
I must be losing my mind. You guys nailed it. Thanks for your help.

Bear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top