Why Dim Arrays as Variants?

hahdawg

Board Regular
Joined
Sep 1, 2011
Messages
51
Suppose I have a bunch of random numbers in A1:C5000, and I want to create an array, call it NumArray, of these numbers.

This code works:

Code:
Dim NumArray As Variant
NumArray=Range("A1:C5000").Value.
.

But this code doesn't work:

Code:
Dim NumArray(1 to 5000, 1 to 3) As Double
NumArray=Range("A1:C5000").Value.
.

Why can't I dim NumArray as a double? I understand that I could dim it as a double, and loop through the range to fill it, but that would be a lot slower. I'm just trying to understand what's going on here.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
Dim NumArray As Variant
NumArray=Range("A1:C5000").Value

Your variant array will contain numbers of subtype double. This is still the fastest way to do this. I'm not sure of the technical reasons. Since we are interfacing between VBA and the Excel worksheet I suppose it's in the way that the values are retrieved and mapped into the array, but your guess is as good as mine.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top