I have received an Excel sheet containing a VBA macro and it gives an error about using ReDim. So to narrow the problem down I have created a simple script that shows the error.
Option VBASupport 1 Type MyTyp a() as String End Type Sub Main Dim typ as New MyTyp ReDim typ.a(0) typ.a(0) = "Hello" Print(typ.a(0)) End Sub
When I run this script I get the following error:
BASIC syntax error. Expected: ,. for the line containing
If I replace the
b.a(0) part with the suggested
b,a(0) (notice the change from the dot to a comma) then ReDim does nothing and I get an out of index error.
The Excel sheet actually contains a variation of the above:
Sub Main Dim typ as New MyTyp With typ ReDim .a(0) .a(0) = "Hello" End With Print(typ.a(0)) End Sub
And that gives me the error:
BASIC syntax error. Symbol expected. at the line with the
I have noticed that if I do this, then it works:
Sub Main Dim a() as String ReDim a(0) as String a(0) = "Hello" Print(a(0)) End Sub
So it seems that I cannot use ReDim with a custom type. Is there any way I can still do this? It would seem silly if
ReDim does not work on dynamic arrays in custom types.
I could replace the
ReDim xxx(0) with
xxx = DimArray(0) but I also have statements with Preserve in it and I don’t know how to fix that.
Thanks for your time!