循环遍历单元格上的一系列单元格,以显示序列中的下一个数字作为单元格的新值

我明白如何循环一个范围,

For Each cell In Range("A1:A5") If [condition] Then End If Next 

而且我知道OnChange事件:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range Set KeyCells = Range("A6") If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then Call Macro End If End Sub 

但是,我不确定如何达到以下效果:我有一个单元格A6,它显示了公式的“结果”,并且我有另一个当前利率的单元格A7用于公式中,我有多个利率经过,所以我宣布他们作为范围(Interest_Rates)。

我的问题是,一旦A6单元格的值发生变化,我怎样才能通过声明的范围去下一个数字,并打印在单元格A7中,以获得范围内的另一个利率的结果,所以我可以打印在表,然后find最高的速度。

必须做出一些假设。 例如,代码假定您的公式在单元格A6中,并且它实际上看起来像这样: =SUM(B3/100*A7) 。 它还假定该公式位于名为“Sheet1”的工作表上,并将结果放在A列中的“公式结果”表中

 Sub tgr() Dim wb As Workbook Dim wsData As Worksheet Dim wsDest As Worksheet Dim rInterestCell As Range Dim rDest As Range Set wb = ActiveWorkbook Set wsData = wb.Sheets("Sheet1") Set wsDest = wb.Sheets("Formula Results") For Each rInterestCell In Range("Interest_Range").Cells wsData.Range("A7").Value = rInterestCell.Value 'Put the interest cell value in range A7, which is used by the formula in A6 wsData.Calculate 'Update the formula result based on the new value in A7 Set rDest = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1) If rDest.Row < 6 Then Set rDest = wsDest.Range("A6") 'Guarantee that A6 is the starting cell for the results rDest.Value = wsData.Range("A6").Value 'Put the value only in a new row in the destination sheet Next rInterestCell End Sub