Pregunta

I am trying to populate a two dimensional array of ranges. I don't know how big the array will need to be, so I am using the ReDim and Preserve functions to dynamically re-size the array as required.

I am encountering runtime error 91: "Object variable or With block variable not set" when I run the code.

I am not an experienced coder, but I have managed to isolate the error, and am sure it is coming from the pseudo code below.

Can anyone see any mistakes I have made that would produce the runtime error?

    Dim ArrayName() as Range
    Dim counter as Integer

    If condition = True Then

        counter = counter + 1

        ReDim Preserve ArrayName(0, counter - 1) 
        ArrayName(0, counter - 1) = Cells(counter, counter) 'I get a runtime error here

    End If

Thank you.

¿Fue útil?

Solución

If you want to store ranges within your array you need to add Set before problem line in this way:

Set ArrayName(0, counter - 1) = Cells(counter, counter)

But if you want to store values of the cells you need to change declaration line to this:

Dim ArrayName() as Double 'or String or Variant depending on value type you need to keep in array
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top