Reading Arrays Into Visual Basic

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
If I have an Excel spreadsheet with named range "Array_1" referring to range A1:A20, what is the most efficient way to get the data from "Array_1" into an array in Visual Basic if I am creating a user defined function that has the text "Array_1" passed into it as a variable?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
In your sub routine that will call your UDF, and with the sheet containing the named range Array_1 active:

Dim vArr as variant
vArr = Range("Array_1").Value
'rest of your code, including a call to the UDF that will pass vArr as the 'argument


vArr is a 2-D array with base 1: 20 x 1 (rows x columns). So, for example, vArr(2,1) =Range("A2").Value (the 2nd cell in Array_1 range.

In your UDF dim the array variable as variant:
Function myFunction(Arr as variant) ...
 
Upvote 0
If I understand you correctly, just a Dim and a Transpose will do the job. Assuming the name of the range comes in as a string called aName, place this at the beginning of the UDF:-
Code:
  Dim aStore As Variant
 
  aStore = Application.Transpose([COLOR=red]ThisWorkbook.Sheets("Sheet1")[/COLOR].Range(aName))

Change the bit in red to point to the correct worksheet.

If you have problems with this, show me your existing UDF code and I'll put it in the right place.
 
Last edited:
Upvote 0
Thanks JoeMo,

this seems to have done the trick.

In your sub routine that will call your UDF, and with the sheet containing the named range Array_1 active:

Dim vArr as variant
vArr = Range("Array_1").Value
'rest of your code, including a call to the UDF that will pass vArr as the 'argument


vArr is a 2-D array with base 1: 20 x 1 (rows x columns). So, for example, vArr(2,1) =Range("A2").Value (the 2nd cell in Array_1 range.

In your UDF dim the array variable as variant:
Function myFunction(Arr as variant) ...
 
Upvote 0
Thanks Ruddles,

I got that to work too. Anyone out there know if there are any differences between these two methods from an efficiency point of view? I am in the process of trying to make some code faster and this function is the building block that others are built off of.
 
Upvote 0
How can I change the array index? If I try to declare the variable as an array I get a compile error "Can't assign to an array". For my purposes, ideally the first item in the array would be 0 instead of 1. I realize I could subtract 1 from the index in the code, but for checking purposes the cleaner code would make things much easier.
 
Upvote 0
Anyone out there know if there are any differences between these two methods from an efficiency point of view? I am in the process of trying to make some code faster and this function is the building block that others are built off of.
I wouldn't rely on someone else's opinion if it's that critical. Write a short piece of code like:-
Code:
dim dtstart as date
dim iptr as long
dtstart=now()
for iptr=1 to 1000000
  call [I]subroutinename[/I]
next iptr
msgbox format(now()-dtstart,"hh:nn:ss")
where subroutinename is a procedure which loads the array.

Try both methods and see which one is the fastest.
 
Upvote 0
How can I change the array index? If I try to declare the variable as an array I get a compile error "Can't assign to an array". For my purposes, ideally the first item in the array would be 0 instead of 1. I realize I could subtract 1 from the index in the code, but for checking purposes the cleaner code would make things much easier.

Hi

Test this:

Code:
Dim arr As Variant
 
arr = Application.Transpose(Range("A1:A3"))
ReDim Preserve arr(0 To 2)
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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