Jump to content


redim preserve variant type

  • Please log in to reply
2 replies to this topic

#1 capr


    Neowinian Senior

  • Joined: 01-July 05

Posted 30 September 2013 - 23:45

Is there a trick to resizing variant type variables? 


I have a pretty simple code

Sub Button1_Click()
Dim y As Variant
y = Range(Cells(1, 1), Cells(2, 4)).Value

ReDim Preserve y(UBound(y, 1), UBound(y, 2) + 1) As Variant

Range(Cells(3, 1), Cells(4, 5)).Value = y

End Sub

the ReDim bit throws an out of scope error. I have to use variant since range.value returns a variant type. 


can you think of a way to fix this? I am reading http://msdn.microsof...e/gg251578.aspx and it says.


"To resize an array contained in a Variant, you must explicitly declare the Variant variable before attempting to resize its array."


I don't know what that means... 

#2 Andre S.

Andre S.


  • Tech Issues Solved: 17
  • Joined: 26-October 05

Posted 01 October 2013 - 03:50

EDIT: forget it, I thought the question was about VB.NET, not VBA.

#3 Kami-


    ♫ d(-_-)b ♫

  • Tech Issues Solved: 3
  • Joined: 28-July 08
  • Location: SandBox

Posted 01 October 2013 - 12:15

Taken from the exact page you linked in your post:


'Similarly, when you use Preserve, you can change the size of the array only by changing the upper bound; changing the lower bound causes an error.'


When you're using your redim you're only trying to set the lower bound use this instead:

ReDim Preserve y(1 To UBound(y, 1), 1 To UBound(y, 2) + 1) As Variant

Alternatively you could use (for when your lowest value isn't 1):

ReDim Preserve y(LBound(y) To UBound(y), LBound(y,2) To UBound(y, 2) + 1) As Variant