11

I used this in Excel and it works fine.

dim varScreen (0 to 2) as string
varScreen(0) = "sample 1"
varScreen(1) = "sample 2"
varScreen(2) = "sample 3"

I am trying to translate this array to VBScript but I keep getting this error:

Line: 14
Error: Expected ')'

I have tried various options, removed as string, dim varScreen as array but I still get the error.

What is the proper syntax?

1
  • There is a space between varScreen and (0 to 2). There should not be. Commented Mar 28, 2015 at 18:00

2 Answers 2

15

You can also create arrays dynamically using the Array function. Sometimes this is more convenient than assigning array elements separately.

Dim arr
arr = Array("sample 1", "sample2", "sample 3")
Sign up to request clarification or add additional context in comments.

1 Comment

Or, if you want to have it all in a single line, use Dim arr : arr = Array("sample 1", "sample2", "sample 3").
11

VBScript's (variables and) arrays can't be typed, so no "as Whatever". VBscript's arrays are zero-based, so no "(x To y)" but only "(z)" where z is the last index (not the size) of the array. In code:

>> Dim varScreen(2)
>> varScreen(0) = "sample 1"
>> varScreen(1) = "sample 2"
>> varScreen(2) = "sample 3"
>> WScript.Echo Join(varScreen, "|")
>>
sample 1|sample 2|sample 3
>>

4 Comments

What are the >> marks for? They make the code a bit harder to read.
>> is the prompt of my REPL.
I encountered similar problem by writing (1 To n), which works fine in vba, I just wanted my array to have base 1, so how can I realize base 1 not from 0? Thanks!
You can't have native base 1 arrays in VBScript. Just adapt your access code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.