在Excel VBA中循环

我有一个逻辑,但不知道如何在Excel中执行/编码。 逻辑如下:

在办公室里,我需要根据自己的购买价值和年龄来找出很多旧文章的价值。 我不想使用VDB或其他内置function。

在我的电子表格中:

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我想计算“ D1 ”年购买价值为“ C1 ”的文章“ A1 ”的折旧值,第一年为@平面“ E1 ”%,以后为10%。

 H1 = "=C1-G1" (Value of the article after depreciation) I1 = "=IF(H1<=F1,F1,H1)" 

请帮我一个macros或公式循环,找出G1的价值。

另外,我想把这个应用到“n”行,因为有“n”个文章。

编辑

@ assylias谢谢你对SO政策的启发,让我为自己找工作find答案。

经过大约30分钟的谷歌search,然后是反复试验,我成功地写了一个macros来做我想做的事情。 这里是:

 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 !

你不需要一个macros,这是简单的math。

假设你的百分比(在E1 )被存储为.2 (对于20%),实际上不是20 ,这个公式将起作用:

 =MAX(F1,C1*(1-E1)-(C1*0.1*(D1-1)))