简体   繁体   English

Vba:使用动态范围查找百分比变化

[英]Vba: Find a percent change using a dynamic range

I have three ranges of data: D14:F28, H14:J28 and L14:N28. 我有三个数据范围:D14:F28,H14:J28和L14:N28。 I wrote a macro that appends a new year of data at row 14, which increases the ranges to: D14:F29, H14:J29 and L14:N29. 我编写了一个宏,该宏在第14行附加了新的一年的数据,从而将范围扩大到:D14:F29,H14:J29和L14:N29。

I'm trying to develop another macro that calculates the percent change for each column in each range and puts the percent change two cells down from the last cell with data in it (eg (D14-D29)/D29 in D31 ect.). 我正在尝试开发另一个宏,该宏计算每个范围内每列的百分比变化,并将百分比变化从最后一个具有数据的单元格向下放置两个单元格(例如(D14-D29)/ D31等中的D29)。 The problem is that I need the formula to be dynamic, so that when the next year of data is appended to the table, the percent change for each column in each new range is put two cells down from the last cell with data in it (eg (D14-D30)/D30 in D32 ect.). 问题是我需要公式是动态的,以便将下一年的数据追加到表中时,每个新范围内每列的百分比变化都将从上一个包含数据的最后一个单元格向下放两个单元格(例如(D32等中的(D14-D30)/ D30等)。 The same thing will happen with next years data when the macro is used, and so on and so forth. 当使用宏时,明年的数据也会发生同样的事情,依此类推。

This is the code that I've come up with: 这是我想出的代码:

Sub Percent_change_Calculation()
iRow = 14
Cell_val = Cells(iRow, 4)

While Cell_val <> ""
    iRow = iRow + 1
    Cell_val = Cells(iRow, 4)
Wend
Cells(iRow + 1, 4) = "=(D14-iRow)/iRow"
End Sub

But it doesn't calculate the percent change and '#NAME?' 但是它不计算百分比变化和“ #NAME?” is entered in the cell. 在单元格中输入。 On the bright side it is in the cell that i want it to be in. 从好的方面来说,我希望它位于单元格中。

Thanks for the help in advance, 我在这里先向您的帮助表示感谢,

Sub Percent_change_Calculation()

    With Cells(14, 4).End(xlDown)

        .Offset(2, 0).Formula = Replace("=(D14 -{a})/{a}", _
                               "{a}", .Address(False, False))
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM