VBA: How to declare a variable and set initial values in one line?

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hi, I want to write variable declaration and set the initial values in one line. I see some examples for that, but it doesn't work in my excel. Here are some examples that failed:
VBA Code:
Dim longArray() As Long = {0, 1, 2, 3}
Dim longArray As Long() = {0, 1, 2, 3}
Dim longArray = New Long() {0, 1, 2, 3}
First and last example are taken from official microsoft documentation website [1] [2]

Any suggestion?
Thank you in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
First and last example are taken from official microsoft documentation website [1] [2]

You will find that both your references relate to .NET

That syntax is not available in VBA.

The below is an extract from link [1] in your post.

1625811233705.png
 
Upvote 0
First and last example are taken from official microsoft documentation website [1] [2]
Hi, as this is not the VBA documentation ‼ …​
So in VBA it needs at least two statements : one to declare the array variable, one to allocate the values :​
Dim V(): V = [{0,1,2,3,4,5}]
 
Upvote 0
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top