插入带除法的简单公式时出现运行时错误 1004
Runtime error 1004 when inserting simple formula with division
我有这个小代码允许根据之前计算的数据填充 table :
(我们对最后一行感兴趣)
'inserts the values calculated
For Each CellCalc In Worksheets(WsPrevData).Range("D" & FirstLine + 2 + NbLinesYear * Counter + 2 * RegionCount - 2 & ":O" & FirstLine + 2 + NbLinesYear * Counter + 2 * RegionCount - 2)
CountCells = 0
CellCalc.Select
For PackOfLines = FirstLine + 2 To FirstLine + 2 + NbLinesYear * Counter - NbLinesYear + 2 * RegionCount - 2 Step NbLinesYear
If IsNumeric(Range(Split(Cells(1, CellCalc.Column).Address, "$")(1) & PackOfLines).Value) Then
CellCalc.Value = CellCalc.Value + Range(Split(Cells(1, CellCalc.Column).Address, "$")(1) & PackOfLines).Value
CountCells = CountCells + 1
End If
Next
pouet = CellCalc.Value
CellCalc.Formula = "=" & pouet & "/" & CountCells 'Runtime error 1004 here -_-
Next
基本上,它只是获取已填充数据中不同单元格中的值,然后对这些单元格进行平均并写入最后一个单元格。
我希望得到值的除法,而不是简单的纯数字。
因此,我使用“.formula”并尝试插入公式。这是当我收到以下错误消息时:
Runtime error 1004: application defined or object defined error
pouet = 120,166907548137
(双)
和CountCells = 3
(整数)
我尝试了不同的东西:
删除“=”符号允许插入公式(变得无用)
直接插入结果有效(但我想要计算的详细信息)
将"pouet"转换为整数使其工作,但我丢失了小数...
另一方面,将两个变量都设置为 Double 并不能使其工作
删除长号中的一些数字没有任何改变
删除斜线没有任何作用
将 CountCells 定义为 double 没有任何改变
使用 .FormulaR1C1 而不是 .Formula 没有任何改变
我已将 Formula
更改为 FormulaLocal
,它非常适合我
Dim pouet As Double
Dim CountCells As Integer
pouet = 120.166907548137
CountCells = 3
Range("A1").FormulaLocal = "=" & pouet & "/" & CountCells
在 VBA 中,小数点分隔符是点,但在某些国家/地区(例如我的国家,我猜也像您的国家),小数点分隔符是逗号。当您使用 Formula
或 FormulaR1C1
时,VBA 需要英文格式的值(并且公式名称必须是英文,参数以逗号分隔,等等),但是如果你使用 FormulaLocal
你可以使用你的区域设置。
上面的代码非常适合我。希望这有帮助
更新:好吧,另一件令人惊讶的事情是:
Range("A1").Formula = "=" & Replace(pouet, ",", ".") & "/" & CountCells
我有这个小代码允许根据之前计算的数据填充 table :
(我们对最后一行感兴趣)
'inserts the values calculated
For Each CellCalc In Worksheets(WsPrevData).Range("D" & FirstLine + 2 + NbLinesYear * Counter + 2 * RegionCount - 2 & ":O" & FirstLine + 2 + NbLinesYear * Counter + 2 * RegionCount - 2)
CountCells = 0
CellCalc.Select
For PackOfLines = FirstLine + 2 To FirstLine + 2 + NbLinesYear * Counter - NbLinesYear + 2 * RegionCount - 2 Step NbLinesYear
If IsNumeric(Range(Split(Cells(1, CellCalc.Column).Address, "$")(1) & PackOfLines).Value) Then
CellCalc.Value = CellCalc.Value + Range(Split(Cells(1, CellCalc.Column).Address, "$")(1) & PackOfLines).Value
CountCells = CountCells + 1
End If
Next
pouet = CellCalc.Value
CellCalc.Formula = "=" & pouet & "/" & CountCells 'Runtime error 1004 here -_-
Next
基本上,它只是获取已填充数据中不同单元格中的值,然后对这些单元格进行平均并写入最后一个单元格。
我希望得到值的除法,而不是简单的纯数字。
因此,我使用“.formula”并尝试插入公式。这是当我收到以下错误消息时:
Runtime error 1004: application defined or object defined error
pouet = 120,166907548137
(双)
和CountCells = 3
(整数)
我尝试了不同的东西:
删除“=”符号允许插入公式(变得无用)
直接插入结果有效(但我想要计算的详细信息)
将"pouet"转换为整数使其工作,但我丢失了小数...
另一方面,将两个变量都设置为 Double 并不能使其工作
删除长号中的一些数字没有任何改变
删除斜线没有任何作用
将 CountCells 定义为 double 没有任何改变
使用 .FormulaR1C1 而不是 .Formula 没有任何改变
我已将 Formula
更改为 FormulaLocal
,它非常适合我
Dim pouet As Double
Dim CountCells As Integer
pouet = 120.166907548137
CountCells = 3
Range("A1").FormulaLocal = "=" & pouet & "/" & CountCells
在 VBA 中,小数点分隔符是点,但在某些国家/地区(例如我的国家,我猜也像您的国家),小数点分隔符是逗号。当您使用 Formula
或 FormulaR1C1
时,VBA 需要英文格式的值(并且公式名称必须是英文,参数以逗号分隔,等等),但是如果你使用 FormulaLocal
你可以使用你的区域设置。
上面的代码非常适合我。希望这有帮助
更新:好吧,另一件令人惊讶的事情是:
Range("A1").Formula = "=" & Replace(pouet, ",", ".") & "/" & CountCells