单元格更改VBA时动态计算值

时间:2018-02-21 02:28:33

标签: excel vba dynamic cell worksheet

所以我正在运行一张包含股票价格实时数据Feed的电子表格。 我已将此写入工作表,以便在价格更新时,头寸的值更新并计算投资组合的新总价值。

所以我有5个主要标题是玩家,股票代码,单位,当前价格,价值。

现在这段代码可以正常运行。当价格更新时,它不会自动更新价值,除非我从单元格来回走动。

Private Sub Worksheet_SelectionChange(ByVal target As Range)
Dim i As Integer, j As Integer, m As Integer, t As Integer
Dim firstrow As Integer, lastrow As Integer
Dim fr As Integer, lr As Integer, lr2 As Integer
Dim sum As Double
Dim name As Range, stock As Range, value As Range, units As Range, cp As Range

With ActiveSheet.Range("A:Z")

    Set stock = .find(what:="Stock Code", After:=.Cells(.Cells.count), LookIn:=xlValues, _
                Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    Set value = .find(what:="Value", After:=.Cells(.Cells.count), LookIn:=xlValues, _
                Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    Set name = .find(what:="Players", After:=.Cells(.Cells.count), LookIn:=xlValues, _
                Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    Set units = .find(what:="Units", After:=.Cells(.Cells.count), LookIn:=xlValues, _
                Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    Set cp = .find(what:="Current Price", After:=.Cells(.Cells.count), LookIn:=xlValues, _
                Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

    fr = stock.Row + 1
    lr = .Cells(.Rows.count, stock.Column).End(xlUp).Row

    If Cells(name.Row + 1, name.Column) = "" Then
        End
    End If

    For i = fr To lr

'checks to ensure that there is a player in the spreadsheet
        If Cells(i, name.Column) <> "" Then
         m = i
        End If

'each player has a total row. The word Total identifies the last row for a player
        For j = m To lr
            If Cells(j, stock.Column) = "TOTAL" Then
                lr2 = j
                Exit For
            End If
        Next j

'checks the range where there are stock codes in the sheet, and when the current price updates it
                If Not Intersect(target, target.Worksheet.Range(.Cells(m, cp.Column), .Cells((lr2 - 1), cp.Column))) Is Nothing Then
                    For t = m To (lr2 - 1)
                        Cells(t, value.Column) = Cells(t, cp.Column) * Cells(t, units.Column)
                    Next t
                End If

'checks to see if there has been a change in the value column for each stock and recalculates the new total
            sum = 0
                If Not Intersect(target, target.Worksheet.Range(.Cells(m, value.Column), .Cells((lr2 - 1), value.Column))) Is Nothing Then
                    For t = m To (lr2 - 1)
                        sum = sum + Cells(t, value.Column)
                    Next t
                    Cells(lr, value.Column) = sum
                End If

    Next i

End With

End Sub

我不确定为什么当前价格更新时,我需要进入价值单元格,然后再实际更新。

感谢任何反馈。

1 个答案:

答案 0 :(得分:1)

抱歉,我明白了。我在Private Sub Worksheet_SelectionChange(ByVal target As Range)而不是Private Sub Worksheet_Change(ByVal target As Range)

下运行它