Difference between assigning an array to another, and not assigning

Chronicles84

New Member
Joined
Apr 8, 2017
Messages
7
Hi,

New to VBA. I'm trying to understand the difference between assigning an array to another and not - the difference syntax and advantages/disadvantages etc

I have two lots of code, as seen below. The first seems to work correctly, and does not rely on assigning parameters to an array:

Code:
Function MyPVArray(R As Double, CFS As Range)
Dim p As Double
Dim n As Long
n = CFS.Cells.Count



For i = 1 To n
    p = p + CFS(i) / (1 + R) ^ n
    Next i
MyPVArray = p


End Function

As far as I can tell this works correctly in determining the present value of a range of cash flows.

The next code tries to replicate this but explicitly assigns the cash flows to an array.

Code:
Function MyPVArray2(R As Double, CFS As Range)


Dim p As Double
Dim n As Long
Dim z As Range
Set z = Range("CFS")
n = z.Cells.Count



For i = 1 To n
    p = p + z(i) / (1 + R) ^ n
    Next i
MyPVArray2 = p




End Function

This second function does not work successfully - returning a value error. I have set ranges before using the set z = ranges("name here") format before, and it hs worked correctly. So, I'm not sure if the problem is here, but this is the only difference between them, and the value error suggests to me that it is not assigning the .value correctly.

Any advice/feedback would be greatly appreciated.

Regards
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,215,798
Messages
6,126,968
Members
449,350
Latest member
Sylvine

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