Cannot assign to a simple array in VBA

JoeS01

Well-known Member
Joined
Jun 25, 2005
Messages
832
I get a VBA compilation error at the first line below shown in red " Cannot assign to array"

Can anyone please suggest where I might be going wrong?

Code:
Dim aryCriticalItems(5) As String
Dim i%, j%
Dim LastRowNo%, RowNo%, ColNo%
Dim ce
Dim myRange As Range
 
[COLOR=red]aryCriticalItems = Array("245-8", "290-8", "315-8", "330-8", "336-8")[/COLOR]
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
It's true, you can't. Try:
Code:
Option Base 1
Sub blah()
Dim aryCriticalItems '(5) As String
Dim i%, j%
Dim LastRowNo%, RowNo%, ColNo%
Dim ce
Dim myRange As Range
 
aryCriticalItems = Array("245-8", "290-8", "315-8", "330-8", "336-8")
End Sub
Option base 1 used because I see you wanted to Dim (5) and you had 5 variables, otherwise you'd have had aryCriticalItems(0 to 4)
 
Upvote 0
The variable aryCriticalItems needs to be dimmed as Variant (not as Variant()).

The function Array always returns a zero based array.
 
Upvote 0
Hi Joe

If you prefer the result to be an array (1 To 5) instead of an array (0 To 4), write at the beginning of the module:

Code:
Option Base 1
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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