excel VBA使用新值在同一单元格中生成公式[已关闭]

tquggr8v  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(153)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
我正在寻找一个VBA脚本,将连续添加新的值,以公式的形式在一个指定的列在工作表中已经存在的值。
例如:如果我将列D指定为包含值的公式所在的列,则会发生以下情况。
1.单元格为空-无React
1.我输入第一个值,即50,按回车确认,公式显示"= 50"
1.我用30覆盖现有值,在旧公式的位置出现一个新公式"= 50 + 30"
1.我添加另一个值在旧的一个即41,一个新的公式是在同一个单元格中创建"= 50 + 30 + 41"。等等自然在单元格中,方程的结果应该显示,而不仅仅是纯文本显示公式的内容。
这是可以实现的吗?

vcudknz3

vcudknz31#

我觉得这正是你需要的

Option Explicit
' created by vtitov.site
Dim oldFormula As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    oldFormula = Target.Formula
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim newVal As Double
newVal = Target.Value
Application.EnableEvents = False
    If oldFormula = "" Then
      Target.Formula = "=" & newVal
    Else
      Target.Formula = oldFormula & "+" & newVal
    End If
Application.EnableEvents = True
End Sub

相关问题