P
Peter T
Thank you all for responding to my post. I wasn't able to read them
That looks OK providing you don't need to establish rngInty is a single row
or column, or to cater for other potential input errors. Otherwise look at
Function pval_Tb() as posted previously with the correction noted by Rick -
If rngIn.Rows.Count > 2 And rngIn.Columns.Count > 1
the '2' should be a '1'.
If you want to stick with what you've got at the very least I'd declare your
variables
Dim counter as Long
Dim Mycell as Range
The function might as well return a Double instead of 'As Variant'
Personally I prefer to assign the result to the function when done so
perhaps -
Dim dblTmp
in the loop: dblTmp = dblTmp etc
when done: PvalCF_Tyrone = dblTmp
If calculation speed is important, if say the input range is a large or you
are using the function extensively, go with RBS's suggestion to assign the
range values to an array -
vArr = rngInty
for each v in vArr
Concerning checking for any text in the range, if that's effectively a
faulty input the error handler in the function I posted will cater for that.
However if you want to differentiate between text cells and other cells
if Typename(Mycell.value) = vbString Then' or TypeName(v)
' it's text
Else
'it's not text, probably a number but could be an error value
End If
Regards,
Peter T
earlier and didn't seem to be able to post anything either. I have
copied my solution below in case anybody wants to look at it. I'm
going to look at your posts tomorrow. I'm sure that there is some
good stuff in them; I have a lot to learn. I addition to what I have
below, I think that I will create a section of code that checks for
text in the range.
Function PvalCF_Tyrone(irate_T As Double, rngInty As Range) As Variant
''''''calculates the present value of a range of cash flows'''
Set myArr = rngInty
For Each Mycell In rngInty
counter = counter + 1
PvalCF_Tyrone = PvalCF_Tyrone + Mycell.Value / (1 + irate_T) ^ counter
Next Mycell
End Function
That looks OK providing you don't need to establish rngInty is a single row
or column, or to cater for other potential input errors. Otherwise look at
Function pval_Tb() as posted previously with the correction noted by Rick -
If rngIn.Rows.Count > 2 And rngIn.Columns.Count > 1
the '2' should be a '1'.
If you want to stick with what you've got at the very least I'd declare your
variables
Dim counter as Long
Dim Mycell as Range
The function might as well return a Double instead of 'As Variant'
Personally I prefer to assign the result to the function when done so
perhaps -
Dim dblTmp
in the loop: dblTmp = dblTmp etc
when done: PvalCF_Tyrone = dblTmp
If calculation speed is important, if say the input range is a large or you
are using the function extensively, go with RBS's suggestion to assign the
range values to an array -
vArr = rngInty
for each v in vArr
Concerning checking for any text in the range, if that's effectively a
faulty input the error handler in the function I posted will cater for that.
However if you want to differentiate between text cells and other cells
if Typename(Mycell.value) = vbString Then' or TypeName(v)
' it's text
Else
'it's not text, probably a number but could be an error value
End If
Regards,
Peter T