【问题标题】:Dynamically Calculating Values when cell changes VBA单元格更改VBA时动态计算值
【发布时间】:2018-07-31 12:35:51
【问题描述】:

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

所以我有 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

我只是不确定为什么当当前价格更新时,我需要在实际更新之前进入价值单元并再次退出。

感谢任何反馈。

【问题讨论】:

    标签: excel vba dynamic cell worksheet


    【解决方案1】:

    对不起,我想通了。我在Private Sub Worksheet_SelectionChange(ByVal target As Range)而不是Private Sub Worksheet_Change(ByVal target As Range)下运行它

    【讨论】:

    • 我打算投票结束这个问题,因为这是一个印刷错误,但经过一番思考,决定让它保持不变。您可以在一天左右的时间内将此标记为“答案”。感谢您自己解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2013-06-02
    相关资源
    最近更新 更多