我有一个逻辑,但不知道如何在 Excel 中执行/编码它。逻辑如下:
在办公室,我需要根据许多旧元素的购买值(value)和年代来了解其值(value)。我不想使用 VDB 或其他内置函数。
在我的电子表格中:
A1 = "Table" (Name of the article)
B1 = "01/01/2005" (Date of purchase in MM/DD/YYYY format)
C1 = "1000" (Purchase value)
D1 = "=DATEDIF(B1,TODAY(),"Y")" (Gives the age of article in years)
E1 = "20" (Percentage depreciation for first year, which varies based on article)
F1 = "=C1*10%" '(Depreciated value should not be less than 10% of purchase value)
现在,在 G1
中,我想计算购买值(value)为“C1
”的商品“A1
”的折旧值“ >D1
"年数,@ flat "E1
"% 为第一年,10% 为后续年份。
H1 = "=C1-G1" (Value of the article after depreciation)
I1 = "=IF(H1<=F1,F1,H1)"
请帮助我使用宏或公式来循环并找出 G1
的值。
此外,我想将其应用于“n”行,因为有“n”篇文章。
编辑
@ assylias感谢您启发我关于SO policy ,让我自己去寻找答案。
经过大约 30 分钟的谷歌搜索和反复试验,我成功编写了一个宏来执行我想要的操作。就这样:
Sub DepVal()
'
' DepVal Macro
' Macro by Prashanth JC
'
' Keyboard Shortcut: Ctrl+d
'
fYear = 0
dVal = 0
tYear = ActiveCell.Offset(0, -3)
purVal = ActiveCell.Offset(0, -4)
depFirst = ActiveCell.Offset(0, -2)
depOther = 10
If tYear >= 1 Then
Do
If fYear = 0 Then
dVal = purVal - (purVal * depFirst) / 100
Else
dVal = dVal - (dVal * depOther) / 100
End If
fYear = fYear + 1
Loop Until fYear = tYear
ActiveCell.Value = dVal
Else
ActiveCell.Value = purVal
End If
End Sub
最后,我将 G1 单元格格式化为小数点后为零的数字。现在一切都很完美!
再次感谢,SO和assylias !
最佳答案
您不需要宏,这只是简单的数学运算。
假设您的百分比(在 E1
中)存储为 .2
(代表 20%),而不是实际的 20
,则此公式将工作:
=MAX(F1,C1*(1-E1)-(C1*0.1*(D1-1)))
关于loops - Excel VBA 中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10725612/