I'm looking to set the values of an Array outside of a procedure in excel but I'm not sure it can be done. This is what I have so far -
Public Names(1 To 5) As String
Set Names = Array("name1", name2", "name3", "name4", "name5")
but the "Set Names" argument gives me an "Invalid Outside Of Procedure" error.
Can this be done ?
Try StreamFab Downloader and download from Netflix, Amazon, Youtube! Or Try DVDFab and copy Blu-rays! or rip iTunes movies!
+ Reply to Thread
Results 1 to 6 of 6
Thread
-
Buddha says that, while he may show you the way, only you can truly save yourself, proving once and for all that he's a lazy, fat bastard. -
Try this ...
Names = Array("name1", name2", "name3", "name4", "name5")
Just remove "Set". But remember... the index starts from zero.
Names(0) = "name1"
Names(1) = "name2" -
Sorry.. I did a test and it bomb.
This will work.... change String to Variant !
Dim Names As Variant
Names = Array("name1", "name2", "name3", "name4", "name5")
Funny thing... declare Names(1 to 5) as String still work but have to assign individually. In this case, the index starts from 1. Whereas for Variant, the index starts from zero. -
Originally Posted by pchan
Buddha says that, while he may show you the way, only you can truly save yourself, proving once and for all that he's a lazy, fat bastard. -
@VCDHunter - I know you're sorted, but for pchan's benefit...
You cannot assign to a static array, only a dynamic one, such as Dim Names() as String. Then if you need a specific size, you can Redim Preserve.
You can also create non-zero index arrays, but the zero index array is standard. -
@tekkieman, Thanks. Is this you are refering static vs dynamic...
Sub test()
'I think you refer to this as STATIC ARRAY !
Dim Names(1 To 5) As String
Names(1) = "name1"
Names(2) = "name2"
Names(3) = "name3"
Names(4) = "name4"
Names(5) = "name5"
'Test dump...
Sheets("Sheet1").Range("A1").Offset(0, 0) = Names(1)
Sheets("Sheet1").Range("A1").Offset(1, 0) = Names(2)
Sheets("Sheet1").Range("A1").Offset(2, 0) = Names(3)
Sheets("Sheet1").Range("A1").Offset(3, 0) = Names(4)
Sheets("Sheet1").Range("A1").Offset(4, 0) = Names(5)
End Sub
Sub test2()
'I think you refer to this as DYNAMIC ARRAY !
Dim Names As Variant
Names = Array("name1", "name2", "name3", "name4", "name5")
'Test dump...
Sheets("Sheet1").Range("A1").Offset(0, 0) = Names(0)
Sheets("Sheet1").Range("A1").Offset(1, 0) = Names(1)
Sheets("Sheet1").Range("A1").Offset(2, 0) = Names(2)
Sheets("Sheet1").Range("A1").Offset(3, 0) = Names(3)
Sheets("Sheet1").Range("A1").Offset(4, 0) = Names(4)
End Sub
Similar Threads
-
multiAVCHD - (RT) Invalid array index. Array is empty - Line 109.
By JackyWang in forum Authoring (Blu-ray)Replies: 3Last Post: 25th Oct 2011, 08:53 -
ffmpeg help... convert mp4 to jpeg array, and back to mp4 - problem!
By SaskVideoMaker in forum Video ConversionReplies: 2Last Post: 4th Feb 2011, 04:53 -
TSPE error in version 0.76 - Index was outside the bounds of the array.
By bildry in forum EditingReplies: 0Last Post: 9th Apr 2010, 14:56 -
BD INfo Error : Index was outside the bounds of the array
By ripper1234 in forum Blu-ray RippingReplies: 0Last Post: 28th Jun 2009, 08:29 -
SATA drive array question
By black cloud in forum ComputerReplies: 8Last Post: 21st Feb 2008, 11:06