I am writing a vba code to calculate the interests paid between two periods
I have written this code but it always give me back the value 0.
Function interests_paid_between(rate As Double, firstPer As Integer, lastPer As Integer, NPER As Integer, PV As Double) As Variant
Dim FutureValue As Double
FutureValue = InputBox("What is the future value of the loan? (if it is 0 put 0 else the value)")
Dim i As Integer
For i = firstPer To lastPer
InterestPaid = InterestPaid + IPmt(rate, i, NPER, PV, FutureValue)
Next i
interests_paid_between = InterestPaid
End Function
Also, I need to include an option where the future value is no equal to 0.
Could you help me with this?
Using Application.InputBox():
Function interests_paid_between(rate As Double, firstPer As Integer, lastPer As Integer, NPER As Integer, PV As Double) As Variant
Dim FutureValue As Double
FutureValue = Application.InputBox("What is the future value of the loan? (if it is 0 put 0 else the value)", Type:=1)
Dim i As Integer
For i = firstPer To lastPer
InterestPaid = InterestPaid + IPmt(rate, i, NPER, PV, FutureValue)
Next i
interests_paid_between = InterestPaid
End Function
In a cell entered:
=interests_paid_between(0.05,1,5,12,-1000)
and inputting 1200 I get:
I don't know if the calculation is correct, but at least it's not zero.
Related
I was trying to make sense of ByVal and ByRef and passing arguments from long to double using the ByVal keyword.
I noticed that VBA gave me the incorrect answer for the value of y squared. It does work when y (i in my sub) is a whole number.
In the below example I had i = 22.5.
The spreadsheet gave me 506.25.
My function gave me 484.
I thought both long and double support decimals.
Sub automation_test()
Dim i As Long
Dim j As Long
Dim x As Long
Dim ans As Long
i = Range("B1")
j = Range("B2")
x = Range("B3")
ans = my_model(i, j, x)
Range("B4").Value = ans
End Sub
Function my_model(ByVal y As Double, ByVal m As Double, ByVal q As Double) As Double
' my_model = (y ^ 2) * (m ^ 3) * (q ^ 1 / 2)
my_model = y ^ 2
End Function
You must declare all used variables As Double (or As Single, depending on the maximum value to be used).
Long variables do not accept decimals.
The difference is exactly the one coming from rounding (down):
22.5^2 = 506.25
22^2 = 484
I am trying to create a Vertical Horizontal Filter (VHF) indicator using Excel VBA.
To make things simple, a n-day VHF is calculated as the ratio of the difference between the maximum of n-period highs and the minimum of n-period lows to the sum of past n-period the absolute value of the first difference of the closing prices:
Function VHF(highs As Range, lows As Range, n As Integer, price As Range, price0 As Range)
Dim count As Long
Dim maxhigh As Double
Dim minlow As Double
Dim closediff As Double
Dim day As Integer
maxhigh = Application.Max(highs)
minlow = Application.Min(lows)
day = WorksheetFunction.count(Range(price0, price))
If day >= n Then
For j = 2 To n - 1
closediff = closediff + Abs(price0.Offset(j, 0) - price0.Offset(j - 1, 0))
Next j
VHF = (maxhigh - minlow) / closediff
End If
End Function
There is some problem with my VBA code, hopefully someone can help me with it thanks!
I have 3 functions:
One converts height in feet and inches over to metres.
One converts weight in stones and pounds over to kilograms.
One calculates the BMI by dividing the weight by height squared.
The problem is, my functions read values from exact cells and place the calculated values into exact cells, which means I can't repeat the calculations for every row below.
See this screenshot:
This is my code:
Option Explicit
Const KgRate As Double = 0.45359237 'number of kg in one pound
Const PoundsInStone As Integer = 14 'number of pounds in one stone
Const InchesInFeet As Integer = 12 'number of inches in one foot
Const CmsInInch As Double = 2.54 'number of centimetres in an inch
Public weightInKilograms As Double
Public finalHeight As Double
**' FUNCTION 1**
Public Function heightInMetres()
Dim numberOfFeet As Integer
Dim numberOfInches As Integer
Dim heightInInches As Integer
Dim heightInCms As Integer
numberOfFeet = Range("C4").Value
numberOfInches = Range("C5").Value
heightInInches = (numberOfFeet * InchesInFeet) + (numberOfInches)
heightInCms = heightInInches * CmsInInch
finalHeight = heightInCms / 100
Range("C7") = finalHeight
End Function
**' FUNCTION 2**
Public Function weightInKilos()
Dim stonesEntered As Integer
Dim poundsEntered As Double
Dim stonesToPounds As Double
stonesEntered = Range("C10").Value
poundsEntered = Range("D10").Value
stonesToPounds = stonesEntered * PoundsInStone
weightInKilograms = ((stonesToPounds + poundsEntered) * KgRate)
Range("E10") = weightInKilograms
End Function
**' FUNCTION 3**
Public Function calculateBMI()
Dim BMI As Double
BMI = weightInKilograms / (finalHeight ^ 2)
Range("F10") = BMI
End Function
**' MAIN PROCEDURE**
Public Sub BMICalculator()
heightInMetres
weightInKilos
calculateBMI
End Sub
1) What is the simplest way to repeat the weight and BMI calculations for the 3 rows below week 1 (which is row 10 in Excel)?
2) Is it possible to (simply) run the code continuously, e.g. as soon as I update the height and weight cells, the calculations are redone?
3) If there are any (simple) enhancements to the code you can suggest, please do. :-)
Thanks in advance,
Pete
I've changed one of your functions here, if you can follow it you should have no problem converting the others:
Public Function heightInMetres(numberOfFeet As Integer, numberOfInches As Integer)
Dim heightInInches As Integer
Dim heightInCms As Integer
heightInInches = (numberOfFeet * InchesInFeet) + (numberOfInches)
heightInCms = heightInInches * CmsInInch
finalHeight = heightInCms / 100
heightInMetres = finalHeight
End Function
So now, just enter the following into the cell eg. C7:
=heightinmetres(C4,C5)
Where the number of feet are in C4 and inches in C5 (in your example)
When G is passed to below code it returns 1 instead of 7. Can anyone point me in the right direction please? I'm trying to get the Excel column index from the column letter. Thank you
Public Function ColLetterToColIndex(colLetter As String) As Integer
colLetter = colLetter.ToUpper()
Dim sum As Integer = 0
For i As Integer = 0 To colLetter.Length - 1
sum *= 26
Dim charA As Integer = Char.GetNumericValue("A")
Dim charColLetter As Integer = Char.GetNumericValue(colLetter(i))
sum += (charColLetter - charA) + 1
Next
Return sum
End Function
Try this (instead of GetNumericValue):
Dim charA As Integer = Asc("A")
Dim charColLetter As Integer = Asc(colLetter)
Everything else can stay the same.
Im having trouble for find last 10% distribution and return Index% 1-10.
just like image below
Thanks Before
Regards
As a result of my understanding you need a formula like this:
(Your sheet starts at A1 and Index(%) start in B2 and end in K2)
=IF(K2>=10;10;IF(SUM(J2:K2)>=10;9;IF(SUM(I2:K2)>=10;8;IF(SUM(H2:K2)>=10;7;IF(SUM(G2:K2)>=10;6;IF(SUM(F2:K2)>=10;5;IF(SUM(E2:K2)>=10;4;IF(SUM(D2:K2)>=10;3;IF(SUM(C2:K2)>=10;2;IF(SUM(B2:K2)>=10;1;0))))))))))
I can give you an answer in VBA like this:
(Alt+F11 to load Visual Basic => Add a module)
Public Function getresult(row As Integer, startCol As Integer, endCol As Integer, resultValue As Integer)
Dim i As Integer, sum As Double
sum = 0
For i = endCol To startCol Step -1
sum = sum + Val(Cells(row, i))
If (sum >= resultValue) Then
getresult = i - 1
Exit Function
End If
Next i
getresult = 0
End Function
and use it like this:
= getresult(ROW();COLUMN(B2); COLUMN(K2);10)