Need help in calculating IRR using custom function in Excel - excel

I am using an excel sheet to track all my investments. In order to calculate my IRR, I need to have the values entered in a specific way for Excel to calculate. So I decided to create a custom function. I will feed this custom function the following values.
Total Investment
Time Period of investment
Final Value of the investment.
I used the following code for creating a custom function. But I get the #VALUE error
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
For n = 0 To (timePeriod - 1)
cashFlow(n) = -1 * eachValue
Next n
cashFlow(timePeriod) = finalValue
ROI = IRR(cashFlow)
End Function
Where is my formula wrong?

Since you tagged it formula:
=IRR(CHOOSE(INT((ROW($ZZ$1:INDEX($ZZ:$ZZ,B2+1))-1)/B2)+1,-1*A2/B2,C2))
Depending on one's version this may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

So you need to reDim it like this:
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
ReDim cashFlow(0 to timePeriod)
'ReDim cashFlow(timePeriod) is also correct - see #Chris Nielsen comment.
For n = 0 To (timePeriod - 1)
cashFlow(n) = -1 * eachValue
Next n
cashFlow(timePeriod) = finalValue
ROI = IRR(cashFlow)
End Function
Notes
-In absence of Option Base statement, array subscripts start at zero.
-Putting both lower and upper limits in ReDim statement is recommended, but if the lower limit is omitted and only the upper limit is specified, the lower limit is taken from the Option Base currently in operation, in this case zero so
ReDim cashFlow(0 to timePeriod)
and
ReDim cashFlow(timePeriod)
are equivalent.

Try below:
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
For n = 0 To (timePeriod - 1)
ReDim Preserve cashFlow(n)
cashFlow(n) = -1 * eachValue
Next n
ReDim Preserve cashFlow(timePeriod)
cashFlow(timePeriod) = finalValue
ROI = WorkSheetFunction.IRR(cashFlow)
End Function

Related

Using a FOR loop within an Excel VBA Function

I created a simple function in MATLAB, and am trying to convert the function into Excel VBA function. My goal is to create an Excel formula =RT('range of dB levels', 'delta-time') and output the estimated reverberation time. The math is simple, see MATLAB code below:
function rr=RT(lvl_broad, dt)
n=12; %number of samples within slope calc
slope=zeros(length(lvl_broad), 1);
for i=1:length(lvl_broad)
if i<((n/2)+1) | i>length(lvl_broad)-(n/2)-1
slope(i)=0;
else
slope(i)=(lvl_broad(i+(n/2))-lvl_broad(i-(n/2)))/n;
end
end
min_slope=min(slope);
rr=abs(dt./min_slope)*60;
end
In excel, I modified/simplified this until I no longer got errors, however, the cell that I enter my 'RT' function in returns #VALUE and I do not know why. Does anything stand out in the code below? (note I changed the input range from lvl_broad to InterruptedNZ)
Function RT(InterruptedNZ, dt)
Dim Slope As Double
Slope = Slope(InterruptedNZ.Height, 1)
For i = 1 To InterruptedNZ.Height
If i < ((6) + 1) Or i > (InterruptedNZ.Height - (6) - 1) Then
Slope(i) = 0
Else
Slope(i) = (InterruptedNZ(i + (6)) - InterruptedNZ(i - (6))) / 12
End If
Next
End
min_slope = Application.WorksheetFunction.Min(Slope)
RT = Abs((dt / min_slope) * 60)
End Function
Here are some tips to translate MATLAB code into VBA code:
length()
If you are trying to get the dimensions of a range, you'll need to use the .Rows.Count or .Columns.Count properties on the range you are working with.
PERFORMANCE NOTE:
When you have a large enough range, it's a good idea to store the values of the range inside an array since you will reduce the number of times you access data from the sheets which can comme with lot of overhead. If so, you'll have to use ubound() and lbound().
zeros()
In VBA, there is no exact equivalent to the zeros() function in MATLAB. The way we would initialize an array of zeros would simply be by initializing an array of doubles (or another numerical type). And since the default value of a double is zero, we don't need to do anything else :
Dim Slope() As Double
ReDim Slope(1 To InterruptedNZ.Rows.Count)
Note that you cannot pass the dimensions in the Dim statement since it only accepts constants as arguments, so we need to create Slope as a dynamic array of doubles and then redimension it to the desired size.
Putting these two principles together, it seems like your code would look something like this:
Function RT(ByRef InterruptedNZ As Range, ByVal dt As Double)
Dim Slope() As Double
ReDim Slope(1 To InterruptedNZ.Rows.Count)
Dim i As Long
For i = 1 To InterruptedNZ.Rows.Count
If i < ((6) + 1) Or i > (InterruptedNZ.Rows.Count - (6) - 1) Then
Slope(i) = 0
Else
Slope(i) = (InterruptedNZ(i + (6)) - InterruptedNZ(i - (6))) / 12
End If
Next
Dim min_slope As Double
min_slope = Application.WorksheetFunction.Min(Slope)
RT = Abs((dt / min_slope) * 60)
End Function
Addtionnal notes:
Refering to cells from a range like this InterruptedNZ(i) works but it is good practice to be more specific like this (assuming column range) :
InterruptedNZ.Cells(i,1)
During my tests, I had a division by zero error since min_slope was zero. You might want to account for that in your code.

How do I instantiate a dynamic, empty array and add its first element?

I've been trying to instantiate an empty array, where I'll be adding elements. For some reason, my script is throwing an error on simply calling Ubound on the empty array. I can't figure out how to instantiate an empty array... Here's what I've got:
Dim data_dates
data_dates = Array("6/24/2019", "7/1/2019", "7/8/2019", "7/15/2019", "7/22/2019", "7/29/2019", "8/5/2019", "8/12/2019", "8/19/2019", "8/26/2019", "9/2/2019")
Dim site_dates
For date_iter = 1 To UBound(data_dates)
If start_date <= data_dates(date_iter) And last_date <= data_dates(date_iter) Then
MsgBox UBound(site_dates) '- LBound(site_dates) + 1
site_dates(UBound(site_dates) + 1) = data_dates(date_iter)
End If
Next date_iter
So that MsgBox line is throwing an error. Is it normal for Ubound to throw an error on an empty array? If so, how do I add the first element to an empty array?
Dim site_dates
This variable is an implicit Variant. While a Variant can very well hold an array, it initializes to Variant/Empty, which isn't an array - that's why UBound(site_dates) is throwing an error: you're trying to get the upper bound of a Variant/Empty, and VBA doesn't know what to do with that.
This declares a dynamic array of Variant items:
Dim site_dates()
That said, in general you should avoid resizing arrays (a loop with ReDim Preserve theArray(UBound(theArray) + 1) is copying the entire array at every iteration just to add a single item - the penalty gets more apparent with more items): if you don't know how many elements you're going to need, it's usually a better idea to use a Collection and Add items as you go. If you do know how many elements you're going to need, then explicitly size the array accordingly, at the declaration site:
Dim site_dates(1 To 10)
Note that Dim statements aren't executable, so you can't use a variable. Use the ReDim statement to do this:
ReDim site_dates(1 To datesCount)
ReDim acts as a declarative statement, so you don't need a prior Dim, even with Option Explicit specified.
In this case you can use Application.WorksheetFunction.CountIf to get the number of dates matching the criteria and size the array before you start iterating the values.
One way to start the growth process:
Sub InTheBeginning()
Dim site_dates() As Date, msg As String
ReDim site_dates(1)
For i = 1 To 10
ReDim site_dates(1 To UBound(site_dates) + 1)
Next i
msg = LBound(site_dates) & vbCrLf & UBound(site_dates)
MsgBox msg
End Sub
First to check that you are using Option base 1 because start your loops with 1??
If you have a setup where you know the max possible site_dates from your data_dates, you can ReDim the site_dates at the beginning (before the loop)to have the same bounds as data-dates.
Keep a track of the number of qualifying items written in a counter in the 'If logic' loop.
Then at the end, you can Redim Preserve down to the amount generated:
ReDim Preserve site_dates(1 to qualifyingCounter)
Alternatively, with no worry about performance you can consider the easiest for me:
Reference mscorlib and use an ArrayList
Dim data_dates
data_dates = Array("6/24/2019", "7/1/2019", "7/8/2019", "7/15/2019", "7/22/2019", "7/29/2019", "8/5/2019", "8/12/2019", "8/19/2019", "8/26/2019", "9/2/2019")
Dim siteDate_ArrayList As New ArrayList
Dim date_iter As Long
For date_iter = 0 To UBound(data_dates)
If date_iter Mod 2 = 0 Then 'I changed this logic just for my test
siteDate_ArrayList.Add data_dates(date_iter)
End If
Next date_iter
Dim site_dates As Variant
'Please note that array resultant from ToArray on an empty ArrayList will have a Ubound of -1
site_dates = siteDate_ArrayList.ToArray
EDIT:
To refernce, go to Tools --> References and look down alphabetically for mscorlib.dll. Then check it.

Access VBA to Excel, add apostrophe [duplicate]

Having a problem with this Error. I am creating a GA and the loop is to assign my fitness value to an array.
some of the variables
Dim Chromolength as integer
Chromolength = varchromolength * aVariables
Dim i as integer, j as integer, counter as integer
Dim Poparr() As Integer
Dim FitValarr() As Integer
the code:
ReDim Poparr(1 To PopSize, 1 To Chromolength)
For i = 1 To PopSize
For j = 1 To Chromolength
If Rnd < 0.5 Then
Poparr(i, j) = 0
Else
Poparr(i, j) = 1
End If
Next j
Next i
For i = 1 To PopSize
j = 1
counter = Chromolength
Do While counter > 0
FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)
j = j + 1
counter = counter - 1
Loop
Next i
I am having problems with:
FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)
I apologize, I am fairly new to VBA.
An overflow condition arises when you create an integer expression that evaluates to a value larger than can be expressed in a 16-bit signed integer. Given the expression, either the contents of FitValarr(i), or the expression 2^(j-1) could be overflowing. Suggest all the the variables presently declared as Int be changed to Long. Long integers are 32-bit signed values and provide a correspondingly larger range of possible values.
I had the same run time error 6. After much investigation l discovered that mine was a simple 'divide by zero' error.
I set up an integer value to hold Zip codes, and Error 6 events plagued me - until I realized that a zip code of 85338 exceeded the capacity of an int...
While I didn't think of a zip code as a "value" it was nonetheless certainly interpreted as one. I suspect the same could happen with addresses as well as other "non-numeric" numeric values. Changing the variable to a string resolved the problem.
It just didn't occur to me that a zip code was a "numeric value." Lesson learned.

vb.net - is it possible to output the first character from a string for monday and then the next character for tuesday (and so on)?

i have a string containing special characters ('★☆☽☾☁') and i would like to have ★ printed out for monday, ☆ for tuesday, ☽ for wednesday, ☾ for thursday, and ☁ for friday. i apologize since i am very new to vb.net so i have only very basic knowledge about it. i have already tried this:
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek
Dim specialcharacters() As Char = "★☆☽☾☁"
If dayIndex < DayOfWeek.Monday Then
txtRandomCharacter.Text = specialcharacters
End If
i would be extremely grateful if anyone could help, thank you!
How's this?
Const specialcharacters() As Char = "★☆☽☾☁"
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek
If dayIndex >= DayOfWeek.Monday andalso dayIndex <= DayOfWeek.Friday Then
txtRandomCharacter.Text = specialcharacters(dayIndex - dayOfWeek.Monday)
End If
It works because the value of DayOfWeek.Monday through DayOfWeek.Friday are sequential.
(Re-edit to correct my previous error of using VBA instead of VB.net)
Yes. Noting that you only have 5 Char in your array (specialcharacters()) I assume you only want to mark Monday to Friday. However, the answer to this question can be extended to cover all week. Using your original code as the base:
Dim txtRandomCharacter As Char = ""
Dim specialcharacters() As Char = "★☆☽☾☁"
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek
If (dayIndex - 1) <= UBound(specialcharacters) Then
txtRandomCharacter = specialcharacters(dayIndex - 1)
End If
However, note the mental gymnastics required in dealing with 0-based arrays.
Dim dayIndex As Integer = Weekday(today,vbMonday) is also valid code.
Explanation for Weekday can be found at http://www.excelfunctions.net/vba-weekday-function.html.
specialcharacters is an array, not a string, so that you can access the array element directly. I have used the UBound function so that you don't accidently get an array out of bounds error by calling a subscript (dayIndex) that is higher than your array is long.
Another option is to use the Mid or Substr function with strings. In this example I have also concatenated some code for brevity.
Dim txtRandomCharacter As String = ""
Const specialcharacters As String = "★☆☽☾☁"
Dim dayIndex As Integer = Date.Today.DayOfWeek ' - DayOfWeek.vbMonday + 1
txtRandomCharacter = If(dayIndex >= 0 And dayIndex <= Len(specialcharacters), specialcharacters.Substring(dayIndex - 1, 1), "X")
The 'X' option in the IIF statement was my addition for testing. You can also use "". Unfortunately, the Substr function in VB.Net is a little less tolerant than the Mid function, hence the additional checks on valid values for dayIndex. And Substr is 0-based.
Using the Mid function, which is tolerant of indexes > length of the string but must be > 0 (hence the If statement):
Dim txtRandomCharacter As String = ""
Const specialcharacters As String = "★☆☽☾☁"
Dim dayIndex As Integer = Date.Today.DayOfWeek ' - DayOfWeek.vbMonday + 1
txtRandomCharacter = Mid(specialcharacters, If(dayIndex > 0, dayIndex, Len(specialcharacters) + 1), 1)
Check your Option Base to see if your arrays start by default at 0 or 1. specialcharacters() could be ranging from 0-4 or 1-5 depending on this setting - which means is may or may not align with the Weekday function which is always in the range 1-7 (or the DayofWeek function which ranges from 0 to 6).
The key point is to understand the difference between a string and an array of characters.
With an array of characters - use array subscripts to select the right member. Check to ensure you are not passing an index that is out of bounds for the array.
With a string, use a Mid or Substr function to get the character from the string. Check to ensure you are not passing an index that is out of bounds.
The other point to recognise is to understand how the days of the week are enumerated. The MSDN reference site only notes the enumeration and does not provide an equivalent integer - the IDE provides some more information. This is important to understand if your counting starts from 0 or 1, and whether you must adjust your index to address this.

Qualifier errors when attempting to debug, along with final lines -- help pls

Below is the code i have put together from various examples to try achieve my goal. Concept is to be dynamic and retrieve from survey sheet within my workbook, to be able to obtain the corresponding TVD for the MD
--Use while loop only to run code if there is a depth in Column B Present. Nested loop uses the difference between depths to calculate a gradient.
---The issue i'm having is getting past my first debug error "Invalid Qualifier".
----Lastly, any suggestions for how i would then return the TVD to Column A, relevant to the looked up MD, within the nested loop to maintain the row in which the MD was grabbed. Sorry for making this so wordy, been working on this for over 10hrs while at work.
http://www.wellog.com/tvd.htm
Sub MdtoTVD()
Dim MD1 As String, MD2 As Integer
Dim TVD1 As String, TVD2 As Integer
Dim Srng As Range 'Survey MD column
Dim MDrng As Range 'MdtoTVD MD column as range
Dim MDdiff As Integer ' Var to calculate difference of MD end from MD start
Dim TVDdiff As Integer ' Var to calculate difference of TVD end from TVD start
Dim TVDincr As Double ' var to use for stepping TVD
Dim MDrow As Integer
Dim i As Long
MDrng = Range("Surveys!B27:B215") 'range from the survey sheet within my report book
Srng = Range("Surveys!G27:G215") 'range from the survey sheet within my report book
Dim X As Integer
X = 2
While Not (IsEmpty(Sheets("MDtoTVD").Cells(X, 2).Value)) 'runs loop as long as there a MD depth to be looked up
Cells(X, 2) = MDrow 'assigns current row value to for MD input
MD1.Value = Application.WorksheetFunction.Index(Srng, Application.WorksheetFunction.Match(MDrow, MDrng, 1)) ' retrieves Start point for MD
MD2.Value = Application.WorksheetFunction.Index(Srng, Application.WorksheetFunction.Match(MDrow, MDrng, 1) + 1) 'retrieves end point for MD
TVD1.Value = Application.WorksheetFunction.Index(MDrng, Application.WorksheetFunction.Match(MDrow, Srng, 1)) 'retrieves start point for TVD
TVD2.Value = Application.WorksheetFunction.Index(MDrng, Application.WorksheetFunction.Match(MDrow, Srng, 1) + 1) 'retrieves end point for TVD
MDdiff.Value = (MD2 - MD1) 'assigns and calculates difference of MD end from MD start
TVDdiff.Value = (TVD2 - TD1) 'assigns and calculates difference of TVD end from TVD start
TVDincr.Value = MDdiff / TVDdiff 'Divides MD by TVD to get increment per foot
For i = 1 To MDdiff Step TVDincr 'set max loop run to amount of feet between survey points
Cells(X, 1).Value = TVD1 + i 'uses the loop to increment the TVD from start point
Next i
Wend
End Sub
I can see a number of problems with your code:
MD1, MD2, TVD1, TVD2 are all of type String. Also, MDdiff, TVDdiff and TVDIncr are all of type Integer. The property Value is not defined for a string or integer variable. Just remove the .Value from all of them and you won't get the "Invalid Qualifier" error.
After you do the above, the following lines will give another error about type mismatch:
MDdiff = (MD2 - MD1)
TVDdiff = (TVD2 - TD1)
because you're trying to subtract a string from another string and assign the result to an integer. Not sure what to advise there, you have to consider what you're trying to achieve and act accordingly. Maybe they shouldn't be strings in the first place? I don't know, up to you to determine that.
At the very least, you can cast strings to integers if you're really sure they're string representations of integers by doing CInt(string_var) or use CLng to convert to long. If the strings are not string representations of integers and you try to cast them to integers, you'll get a type mismatch error.
When you assign a value to a Range object, you need to use Set. So do:
Set MDrng = Range("Surveys!B27:B215")
Set Srng = Range("Surveys!G27:G215")
to correctly set the ranges.
Another problem is that you haven't assign a value to X but you use it as a cell index. By default, uninitialised numeric variables in VBA get assigned the value of 0, so doing .Cells(X, 2) will fail because row 0 is not a valid row index.
In this line:
TVDincr = MDdiff / TVDdiff
you're dividing two integers and you assign the result to another integer. Note that if the result of the division happens to be a decimal (like 3 / 2 = 1.5), your TVDincr integer will actually contain just 1, i.e. you lose some precision. I don't understand your code to know if it's ok or not, you have to judge for yourself, I'm pointing it out just in case you're not aware of that.
Also, if TVDdiff happens to be 0, then you'll get a "division by zero" error.
This line in your For loop:
Cells(X, 1).Value = TVD1 + i
will also generate an error, because you're trying to numerically add TVD1 (a string) and i (a long). Perhaps you're trying to concatenate the two, in which case you should replace + with &.
There's also a problem when calling the WorksheetFunctions, but I haven't been able to determine the cause. Probably if you fix the other errors then it'll be easier to understand what's going on, not sure though. You just have to investigate things a little bit too.

Resources