Forgot your password?
Or sign in with one of these services
capr, 30 Sep 2013
Posted 30 Sep 2013
Is there a trick to resizing variant type variables?
I have a pretty simple code
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
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.microsoft.com/en-us/library/office/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...
Posted 1 Oct 2013
EDIT: forget it, I thought the question was about VB.NET, not VBA.
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
You need to be a member in order to leave a comment
Sign up for a new account in our community. It's easy!
Already have an account? Sign in here.
No registered users viewing this page.
Existing user? Sign In