Excel VBA:公式对于对象来说太复杂了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17349817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:54:58  来源:igfitidea点击:

Excel VBA: Formula is too complex for object

excelvbabuttonexcel-vba

提问by user2529177

This may seem like a simple problem, but it's been very frustrating. I've written a couple (relatively simple) macros in VBA for a spreadsheet that I want to make very user-friendly.

这似乎是一个简单的问题,但它非常令人沮丧。我在 VBA 中为电子表格编写了几个(相对简单的)宏,我想让它非常用户友好。

In other words, I want all macros to be run by buttons. However, whenever I try to assign buttons to macros I am getting the error:

换句话说,我希望所有宏都由按钮运行。但是,每当我尝试将按钮分配给宏时,我都会收到错误消息:

Formula is too complex to be assigned to object

公式太复杂,无法分配给对象

This happens even when I try to assign it to an empty subroutine. It worked, but only for the first macro I wrote. Once I added other subroutines to the module, I could no longer assign new buttons to macros.

即使我尝试将其分配给空子例程时也会发生这种情况。它有效,但仅适用于我编写的第一个宏。一旦我向模块添加了其他子程序,我就无法再为宏分配新按钮。

This is the code I've written:

这是我写的代码:

Sub Button1_Click()

selName = Range("C2").Value
div = Range("E2").Value
cost = Range("F2").Value
diff = div - cost

If diff < 0 Then
    diff = 0
End If
Range("G2").Select
ActiveCell.FormulaR1C1 = diff

x = Range("A2").Value + 1
Worksheets("VIP_TEMPLATE.PIVOT").Select
Range("J" & x).Select
ActiveCell.FormulaR1C1 = diff

Worksheets("CALCULATE").Select
Range("F2").Select
ActiveCell.ClearContents

End Sub

I have another subroutine in the module, which I think may be contributing to the problem? I think this because when I simply copy-pasted my data into a new worksheet, I could assign a button to the routine. I don't think it has anything to do with my file path because I don't use any (I think) offending characters.

我在模块中有另一个子程序,我认为这可能是导致问题的原因?我认为这是因为当我简单地将数据复制粘贴到新工作表中时,我可以为例程分配一个按钮。我认为这与我的文件路径没有任何关系,因为我不使用任何(我认为)有问题的字符。

回答by user2529177

I have found the problem. It turns out that I did actually have brackets in my file path (buried way in there).

我找到了问题所在。事实证明,我的文件路径中确实有括号(埋在那里)。