Dim and ReDim with Option Explicit

igold

Well-known Member
Joined
Jul 8, 2014
Messages
3,410
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I was reading a posted question on the Forum, and in this particular post, the OP had pasted the entire code starting with the "Option Explicit" at the top. As I read the code I noticed that the OP had not Dimensioned an Array, but only Re-Dimensioned it. I thought that the code would not compile, but it did. I tested it myself and to my surprise there is no need to initially Dimension an Array, but rather if you ReDimension it, that is sufficient. As an example this code will compile.

VBA Code:
Option Explicit

Sub test()
    ReDim arr(1 To 10, 1 To 2)
End Sub

Although I do not have any questions, if anyone wants to comment or provide some insight on this, that would be great.
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
My comment:

with ReDim the array is not only re-dimentioned but also (if it was not declared before) is also declared.
Option explicit forces declaration of all variables before their first use, and as said redim declares variable (if it was not declared before) so it is fine.

Use of arr before redim will cause an error, but use after that will not.

Invalid with Option explicit
VBA Code:
Sub test()
    arr(1, 1) = 0
    ReDim arr(1 To 10, 1 To 2)
End Sub
valid
VBA Code:
Sub test()
    ReDim arr(1 To 10, 1 To 2)
    arr(1, 1) = 0
End Sub

similar situation with 1 dimentional variable
VBA Code:
Sub test()
    i = 2
    ReDim arr(1 To 10, 1 To 2)
    arr(1, 1) = i
    Dim i As Integer
End Sub
even if i is declared, it is declared after first use, so causes a compiler error if Option Exclicit was used
Moreover, if such a code will be used without option explicit, one will get "duplicate declaration in current scope" error, because i has been automatically declared before first use in assignment (i=2) line (as type variant containing integer data)
 
Upvote 0
I do understand that it works, but perhaps we should clue Microsoft in as well. The following is a quote from docs.microsoft.com

The ReDim statement is used to size or resize a dynamic array that has already been
formally declared by using a Private, Public, or Dim statement
with empty
parentheses (without dimension subscripts)
. Use the ReDim statement repeatedly to
change the number of elements and dimensions in an array
 
Upvote 0
Well, it's almost a philosophy

But is it a first time M$ declares something to be "rather strict" but then it shows to be "quite relaxed"...


So the above post is about "how do I understand why it works" not an official explanation :-)

And going to yout opening post - rather comment than insight.
 
Upvote 0

Forum statistics

Threads
1,223,323
Messages
6,171,458
Members
452,405
Latest member
DiamondHand_Jo

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