I need to display the year and the value it lost in that particular year - basic

I am in need of your assistance with my visual basic code. I need to display the double depreciation of a product. Each year the product will lose value. I need to display the year and the value it lost in that particular year using Visual Basic
I have the Interface the code but when I run the code I get incorrect output.
For example:
Product cost is R 5000
Useful Life is 5 Years
when I apply the formula to the problem the answer should be:
1 should be 2000
2 should be 1200
3 should be 720
4 should be 432
5 should be 259.20
With my code I do get year 1 correct but years 2 - 5 have the same answer as year 1.
I have used a for loop to do the calculations but it seems to be stuck at year 1.
Any assistance will be appreciated as I am completely new to programming.
Calculating the Depreciation
Sub Calculations()
For yr As Integer = 1 To Life
If yr <= Life Then
Depreciation = (2 / Life) * Cost
lstBalances.Items.Add(yr & " " & Depreciation)
End If
Next
End Sub

You're not adjusting the cost for the next calculation. Also your line If yr <= Life Then is redundant. The scope of the loop is already defined in the For/Next context and yr will always be less than or equal to Life.
Sub Calculations()
For yr As Integer = 1 To Life
Depreciation = (2 / Life) * Cost
lstBalances.Items.Add(yr & " " & Depreciation)
Cost = Cost - Depreciation
Next
End Sub

Related

Placing arrays in cells with Excel VBA

I use the following Excel custom function to put a value into cell based on a start and end date, and a frequency. The spreadsheet is for projecting costs into the future, so I may allocate $10k every 5 years starting from 2022 and ending in 2072 (eg 2022, 2027, 2032....2067, 2072). I also index it also sometimes.
Current Macro
Public Function spend(FY, start, finish, frequency, amount, index, base)
'Check if FY is within range if not return nothing and exit
If FY < start Or FY > finish Then
spend = ""
Exit Function
End If
'Check frequency to see if valid year
If FY = start Then
spend = amount * (1 + index) ^ (FY - base)
ElseIf (FY - start) Mod frequency = 0 Then
spend = amount * (1 + index) ^ (FY - base)
Else
spend = ""
End If
End Function
Current Spreadsheet
For some larger projects, the costs may spread over multiple years (eg Year 1:$100k, Year 2:$500k, Year 3:$600). Is there a way to put a series of numbers into "Amount" cell. For example if this project repeats every 10 years from 2022 simliar to below:

Trying to show a full years amount based on a smaller fraction of the year's total

this is my first post.
Right now, I have a limited set of data ranging from the beginning of this financial year until now. I'm trying to show what a full year's worth of that data would look like.
For example, if the number is at 10, and the data range is from 1/07/2021 - 30/12/2021 (half the year), then the end output should be 20. Or for example, turn a 12 with 3/4 of the year date range to 16 for a full years' worth.
However, my current formula would end up with 15 (10 + "half") rather than (10 + 10)
Right now this is what I have, but I know there's something off on my logic, as the output is smaller than it should be:
D1+((364-(F1-E1))/365)*D1
where E1 is the start date and F1 is the end date, and d1 is the number for that date range
Thanks in advance
endDate - startDate will give you the number of days the data covers.
(endDate - startDate) / 365 will give you what fraction of a year the sample represents.
Let’s say this works out to be 30%, or 0.30.
annualValue * 0.30 = periodValue and therefore we know that periodValue / 0.30 = annualValue.
So there it is, the cell you want the Annual Value in should be:
= periodValue / ( ( endDate - startDate) / 365 )
I will leave it to you to replace each of the three named values in my example to be the correct cell references. I suspect that’s probably:
=D1/((F1-E1)/365) which is the same as (D1*365)/(F1-E1).
The easy way to remember this is that it’s just cross-multiplication.
periodValue / days is proportionate to annualValue / 365. Thus periodValue / days = annualValue / 365. Cross-multiply and you get periodValue * 365 = annualValue * days. Divide both sides by days and you get `annualValue = (periodValue * 365)/days.

Formula to calculate overtime hours not working consistently

Edit: It now calculates overtime after implementing Vincent's solution in the comments
My girlfriend works from home 6 hours a day and gets to make her own hours. She asked me to make a time sheet for her that she can enter her start and end times into and will automatically calculate her hours. It calculates the total hours worked no problem (so long as they don't go pass midnight), but I wanted to also have it track overtime hours for her.
I have a formula that should work, but the problem is that if all the start and end times on a given day are both AM or both PM then it will not calculate the overtime hours no matter how much the total hours surpass 6 hours. However, as soon as you enter in a time value that starts at an AM time and ends at a PM time, such as 11:00 to 14:00, then it calculates the total overtime hours for the day just fine.
Here's a screenshot of the time sheet with times entered with total hours above 6, but no start and end time crossing from AM to PM:
Here's an example where the the last time entered on the first day now goes from an AM time to a PM time. Notice that the overtime hours are now being calculated accurately.
The VBA generating the formulas that calculates the hours looks like this:
Sub calcHours(ByVal numDays As Integer)
Dim newWeekRow As Integer: newWeekRow = 0
With Sheets("timesheet")
'Add the formula to calculate hours for the day
Range(Cells(5, 17), Cells(numDays + 4, 17)).FormulaR1C1 = _
"=SUM((RC[-2] - RC[-3]) + (RC[-4] - RC[-5]) + (RC[-6] - RC[-7]) + (RC[-8] - RC[-9]) + (RC[-10] - RC[-11]) + (RC[-12] - RC[-13]))"
'Add formula to calculate overtime hours
Range(Cells(5, 19), Cells(numDays + 4, 19)).FormulaR1C1 = _
"=IF(RC[-2]-(6/24) > 6/24, RC[-2]-(6/24), 0)"
'Add the formula to calculate hours for the week
For ctr = 1 To numDays
If (Cells(4 + ctr, 2).Value = "Saturday") Then 'found the end of the week
If (newWeekRow = 0) Then 'end of the first week
Cells(4 + ctr, 18).FormulaR1C1 = "=SUM(R5C[-1]:RC[-1])"
newWeekRow = 4 + ctr
Else
Cells(4 + ctr, 18).FormulaR1C1 = "=SUM(R[-6]C[-1]:RC[-1])"
newWeekRow = 4 + ctr
End If
End If
If (ctr = numDays) Then 'reached the end of the last week
Cells(4 + ctr, 18).FormulaR1C1 = "=SUM(R" & newWeekRow + 1 & "C[-1]:RC[-1])"
End If
Next ctr
'Add formula to calulate total hours for the month
With Cells(5 + numDays, 17)
.FormulaR1C1 = "=SUM(R[-" & numDays & "]C:R[-1]C)"
.Font.Bold = True
End With
End With
End Sub
Currently your formula for suming the hours in the week is essentially:
=SUM(E5-D5,G5-F5,I5-H5,K5-J5,M5-L5, O5-N5)
The problem is that when you go past midnight you have no way of indicating that the time is the following day. Since Time is stored as a decimal and date is stored as the integer, and by default, when only time is entered, 0 is the integer for the day. All you need to do is add 1 to the time when they out time is less than the in time. Going to cheat a bit and assume there will never be more than a 24 hour period between in and out. So with that cheat in mind and that boolean True and False get converted to 1 and 0 respectively when going through a math operator, you could adjust your formula as follows:
=SUM(E5+(E5<D5)-D5,G5+(G5<F5)-F5,I5+(I5<H5)-H5,K5+(K5<J5)-J5,M5+(M5<L5)-L5, O5+(O5<N5)-N5)
Alternatively you could record the hours on the day they were worked on, not broken by the time you slept overnight as the delineater for the day. Ie if you work from 2300 on monday to 0100 on tuesday, then record monday as time in 2300 and time out as 23:59, and Tuesday record time in as 00:00 and time out as 01:01. Note the added 1 minute to the time out. This is to make up for the missing minute between 23:59 and 24:00 (which does not technically exist in excel but does work with some operations)
As for the overtime calculation see Vincent G's comment. Another spin on that formula could be:
=MAX(Q5-6/24,0)
You will need to change the cell references to match your VBA programming needs. Both formulas correct overtime issue calculation and start and end time crossing midnight.
First, if you go over midnight, here's what I would do:
Going over midnight means the hours will be negative. In that case, do an IF statement where if the result is a negative number, simply add 24 hours (or 1 day) to the result.
Second, couldn't you calculate the overtime without VBA by simply subtracting 6 hours from total working hours for the day?
I'm not too familiar with VBA, so if I'm missing something blatantly obvious, please forgive me.

Generate "duplicate" row if between a date there is more than one month involved

I'm asked to "automatize" some Excel tasks regarding medical licenses, what I mainly need right now is to identify if a license is longer than a month period, if so, duplicate the entire row data besides the date which needs to be adapted on the "next month" and also generate a period id that includes the year + month (e.g. 201801 for a license started in Jan/2018).
What I did first was checking and displaying if a license is longer than x period (did a loop that counts days and identify if it matches with the month above the cell), so if one started on Jan and ended in Feb it would show like this:
period id member start date end date jan feb
201801 john doe 09/01/2018 07/02/2018 23 7
=SUMAPRODUCTO(--(TEXTO(FILA(INDIRECTO($J9 & ":" & SI($K9="";HOY();$K9)));"MMM")=N$8))
The problem with the table above is that the file becomes humongous with the formula, so instead I'm thinking, for now, just the row duplication:
period id member start date end date
201801 john doe 09/01/2018 31/01/2018
201802 john doe 01/02/2018 07/02/2018
The thing is I can't figure how to compare months and print them the way I have to.
Sub CopyData()
Set hojatst = Sheets(Hoja5)
Dim d
Dim j
d = 1
j = 8
Do Until IsEmpty(hojatst.Range("D" & j))
If hojatst.Range("D" & j) < hojatst.Range("E" & j) Then
d = d + 1
End If
j = j + 1
End Sub
This is a 'sketch' of the code you will need to write. Stackoverflow is not a code request service and so you need to do your best to write it yourself. The community is happy to help you troubleshoot while you learn, but you should be using Google a lot. I wrote out the sketch to get you started because I remember how hard it was to learn the basics of coding. Feel free to ask clarifying questions.
First of all I cannot get Excel to recognize the DD/MM/YYYY format. It might be because mine is a US copy and that is not a standard time format here. You seem to be using a different version of Excel due to the formulas you listed. However be sure to double check that your code is correctly reading the DD/MM/YYYY format as you proceed.
Create a variable cl that is your current line. Create md which is the month difference.
If Month (Start Date) is less than Month(End Date) Then md = Difference of Months, insert that many lines below the current line. Range("E" & cl+md).Value = Range("E" & cl)which pastes your end date to the last slot.
Then for cl < cl+md fill in the periods by add +1 to the value if the last number is not 4 and +97 if it is.
Then fill in the blank cells for the dates with the Start of Month and End of Month dates.
Finally set cl = cl + md + 1 and loop to move on to the next 'original' line.

Simplyfying FV with monthly payment calculation

7000
10008.54
13665.45
18110.44
23513.35
30080.62
38063.19
47766.04
59559.92
73895.45
91320.39
112500.50
In the above list, 7000 is stationary. From 10,008.54 to downwards, I have the following formula pulled down with $N$27 containing 1500 (does not change)
=((((M27*1.05)*1.05)*1.05)*1.05)+$N$27
This essentially calculates 7000 compounded 4 times monthly with 1500 added at the end of period. In excel I was able to come up with this
=FV(P32,P29,-N27)+(P31*POWER((1+P32),P29))
P32 = .05
P29 = 48
N27 = 1500
First FV calculates the FV of 1500 end of 48 periods with interest rate of 5%
The second part simplifies the table above. However the results differ by huge margin.
Question being: How else can I show my list above in short simple formula. I want the beginning of the month value compounded 4 times/weekly the following month + a payment added at the end of the month for up to 1 year.
Any leads and your time is appreciated.
I will put this out there. This is a UDF that will do what you want. I realize that you did not ask for a vba answer but just in case you are open to one:
Function FVSPECIAL(rate As Double, nper As Double, PV As Double, PMT As Double, PMTEvry As Double)
Dim i As Integer
Dim otpt As Double
otpt = PV
For i = 1 To CInt(nper / PMTEvry) - 1
otpt = otpt * (1 + rate) ^ (PMTEvry) + PMT
Next i
FVSPECIAL = otpt
End Function
Add a module to the workbook and paste this into it.
Then call the function. To call it you would, using the cells from the question
=FVSPECIAL(P32,P29,M27,N27,4)
It basically is FVSPECIAL(Rate,Nper,PV,PMT, # of NPer per payment)

Resources