VBA - Use part of an array

ojhawkins

New Member
Joined
Nov 17, 2011
Messages
39
Hi All,

I have an array basically like this

Dim arr(1 to 500)

For i = 1 to 500

Is there any way I can use part of it to make a calculation?

For example I want to calculate the average on the first 250 values, so i=1 to i=250?

Any help is greatly appreciated

Cheers
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi All,

I have an array basically like this

Dim arr(1 to 500)

For i = 1 to 500

Is there any way I can use part of it to make a calculation?

For example I want to calculate the average on the first 250 values, so i=1 to i=250?

Any help is greatly appreciated

Cheers
First 250 values? It sounds like you have simplified your request for us. Doing that would pretty much mean any solution we give you would not be easily modifiable by you to handle what I suspect is a more complex selection of cells. Can you verify that all you really want is to average the first 250 cells and nothing more? If you do want something more, please tell us exactly what so we can give you a usable solution.
 
Upvote 0
Yes correct,

I am calculating stock price movement and then want to calculate both 130 day and 260 day implied Vol so instead or creating 2 arrays of 130 and 260 length

I want to store 1 array and use it to do both calculations

below is the calculation which I can get success fully but need to create 2 arrays or erase the Array for the second calculation

arr(i) = WorksheetFunction.Ln(Sheets(MetWScount).Cells(i, 7).Offset(1, 0) / Sheets(MetWScount).Cells(i, 7).Offset(2, 0))

tdv130 = WorksheetFunction.StDev(arr) * (260) ^ 0.5
 
Upvote 0
Howdy

You can use Index to return the elements you need:

Code:
Msgbox "Average of first 250 elements is " & Application.Average(Application.Index(arr,Evaluate("ROW(1:250)")))

MsgBox "Average of next 250 elements is " & Application.Average(Application.Index(arr,Evaluate("ROW(251:500)")))

I'm not sure this would be any faster at the end of the day than if you simply used two arrays though - Rick may be better able to answer that than I.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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