Excel visual basic 1004错误,对象'范围'的方法'值'失败

我为excel做了一些插件。 但由于某种原因,它失败了。 重新启动后,它的工作正常,但是当我复制粘贴文本到Excel中,并尝试运行它,它给了我错误:运行时'1004',对象'范围'的方法'价值'失败。

我想要做的是退出简单。 我喜欢在前面使用'='来构build公式:(B5 + B6)/ 2,所以Excel不计算这些expression式。 我结束了一个大列,结束后,我想select计算列的第一个单元格,激活我的加载项,他在前面放一个'=',并向下循环直到一个空的单元格。 这样我的列中的每个单元格现在计算。

我迷路了,你能帮我吗?

Sub makeFormula() Do ActiveCell.Value = "=" & ActiveCell.Value ActiveCell.Offset(1, 0).Select Loop While ActiveCell.Value <> Empty end Sub 

我find了解决scheme,通过debugging,我发现逗号是问题,所以我将逗号更改为点,然后计算。 现在它像一个魅力。

 Sub makeFormula() Dim Temp As String Do Temp = ActiveCell.Value2 Temp = Replace(Temp, ",", ".", 1) ActiveCell.Formula = "=" & Temp ActiveCell.Offset(1, 0).Select Loop While ActiveCell.Value2 <> Empty End Sub 

感谢您的所有build议。

 Sub makeFormula() Dim c as range Set c = selection.cells(1) 'in case >1 cell is selected do while len(c.value) > 0 'need to put quotes around the value if not a number 'c.Formula = "=""" & c.Value & """" 'use this if the value is a valid formula without = c.Formula = "=" & c.Value Set c=c.Offset(1, 0) Loop End Sub 

您需要将您的值传递给临时stringvariables。 为我工作:

 Sub makeFormula() Dim Temp As String Do Temp = ActiveCell.Value2 ActiveCell.Formula = "=" & Temp ActiveCell.Offset(1, 0).Select Loop While ActiveCell.Value2 <> Empty End Sub