【问题标题】:Need macro to write the formula in a cell not the answer (ouput)需要宏在单元格中写入公式而不是答案(输出)
【发布时间】:2021-06-28 04:17:47
【问题描述】:

我无法弄清楚 - 我要么得到一个命名?尝试所有操作时出现错误或应用程序定义的错误。

Dim sAmt As Integer
Dim sOriginalAmount As Integer
sOriginalAmount = ActiveCell.Value
sAmt = Application.InputBox("How much additional cost?", Type:=2)
ActiveCell.Formula = sOriginalAmount + sAmt

我希望 Excel 做的是将公式写入单元格而不是输出。

例如 - 目前如果我的 sOriginalAmount 为 15,我输入 sAmt 为 20,在运行代码时,它会在单元格中给出答案 35,但我希望它写 =sum(valueofsOriginalAmount + valueofsAmt) 以便稍后我可以看到原始金额是多少以及添加了多少。

有人可以帮忙吗?我已经尝试过 & "" 的各种组合,但我就是无法得到它。对于这样一个菜鸟问题,我深表歉意,但我是自学的,我只为我的工作编写代码。

【问题讨论】:

    标签: excel-formula output formula cell write


    【解决方案1】:

    这应该可行。

    ActiveCell.Formula = "=" & sOriginalAmount & "+" & sAmt
    

    或者如果你真的想使用 SUM。

    ActiveCell.Formula = "=SUM(" & sOriginalAmount & "," & sAmt & ")"
    

    【讨论】:

    • 工作就像一个魅力!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多