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

时间:2012-06-08 20:23:25

标签: excel vba

我的任务是为一列数据创建一种方法来更新底部单元格的最新条目。更具体地说,在今年的每个月输入贷款组合金额,最近的条目也需要出现在列的底部。这是我最初提出的内容,但这不适用于底部之前的最后一个条目。

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

1 个答案:

答案 0 :(得分:0)

我认为低于最新月份总数的单元格将始终为空白,除非是12月。否则我不知道你怎么知道哪个是最新的:

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