The problem is in your declarations
Dim constants() As Double 'this declares the dynamic array
but you ReDim
a different undeclared array
ReDim constans(0) 'this resizes the array(rediminsion's the array)
Adding the missing "t" to "constans" will fix that. If you want to avoid inadvertently using undeclared variables then go to Tools..> Options..>Editor tab and make sure the "Require Variable Declaration is checked - this will insert Option Explicit
at the top of every module you create (but not your existing ones).
With Option Explicit
and CamelCase variables declared, as you enter your variable names in lower case (i.e. camelcase
) the next time you press Enter, they will change to CamelCase
as declared confirming that the name entered has been declared. If it doesn't change then it's undeclared and it will cause a compile error when you try to run the procedure.
Before you run your procedure, compile it by going to Debug..> Compile VBA Project or press Alt > d > l. If you don't see a message box, then it will probably run
PS: You may get unexpected results with your current loop because my testing showed constants(0) = 1 and so on until constants(last i) = 0. Probably not what you want but if it is, then...all good!