当列中的值发生更改时更新特定的单元格

我的任务是创build一个数据列的方法来更新最近的条目到底部的单元格。 更具体地说,为今年的每个月input贷款组合数量,最近的条目也需要出现在列的底部。 这是我初步想出来的,但是这对底部之前的最后一项不起作用。

Private Sub Worksheet_Change(ByVal Target As Range) xC = 0 yC = 7 If (Target.Column = 3) Then Do prevInt = currentInt currentInt = Sheet1.Cells(yC, 3).Value If (currentInt = 0) Then Sheet1.Cells(19, 3).Value = prevInt xC = 1 End If yC = yC + 1 Loop Until xC = 1 End If End Sub 

我假设最近一个月以下的单元总是空白,除非是十二月。 否则我不知道你怎么知道哪一个是最新的:

 Public Sub SetTotalCellValue() Const portfolioColumn As Integer = 3 Const endRow As Integer = 14 'row of "total" cell Dim sheet As Worksheet Dim rowCount As Integer Dim val As Object rowCount = 2 'start at January, skip header row 'Set sheet = some sheet 'find last empty cell For a = 1 To endRow If sheet.Cells(a, portfolioColumn).Value = Empty Then sheet.Cells(endRow, portfolioColumn).Value = sheet.Cells(a - 1, portfolioColumn).Value Exit For ElseIf a = endRow - 1 Then sheet.Cells(endRow, portfolioColumn).Value = sheet.Cells(a, portfolioColumn).Value Exit For End If Next a End Sub