9

Does anyone know why this doesn't throw a Variable not defined error when I compile it?

'Class1.cls'
Option Explicit

Public Sub foo()
    ReDim fubar(1 To 2, 1 To 1)
End Sub

Am I misunderstanding how Option Explicit is supposed to work? Or is there something wrong with this test? Or is this just a bug in VBA?

(I am testing this on Excel 2007)

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • 1
    ReDim is a standard (implicit) procedure, so you are just passing the arguments to a proc; Option Explicit does not trigger in this case. – Alexander Bell May 10 '13 at 17:01
  • @AlexBell No, without the ReDim, passing an undeclared variable to a proc still generates the "undeclared variable error". I think that Adrien Lacroix has it right. – RBarryYoung May 10 '13 at 17:11
  • Yeah, I agree! Anyway, the final conclusion: it's not a bug, but a documented behavior. – Alexander Bell May 10 '13 at 17:15

1 Answers1

12

From MSDN (http://msdn.microsoft.com/en-gb/library/y9341s4f%28v=vs.80%29.aspx)

"When Option Explicit appears in a file, you must explicitly declare all variables using the Dim or ReDim statements."

So ReDim works in Option Explicit.

Adrien Lacroix
  • 3,502
  • 1
  • 20
  • 23