0

我试图让我的宏将公式放入特定单元格中,其单元格引用由循环定义,并且公式数据的范围(对于 STDEV.P)基于循环的进度。到目前为止,我所做的代码能够在我希望添加 STDEV 的位置插入一行,在单元格 A(x) 中输入文本“StdDev”,然后选择该新行中的每个单元格(增加列)。代码如下所示:

firstCell = 2
lastCell = 2
initialValue = 84
stepSize = 5
lastRow = Range("A1").End(xlDown).Row
lastColumn = Range("A1").End(xlToRight).Column
For x = 2 To lastRow
    cellDifference = Range("B" & (x)).Value - initialValue
    If Abs(cellDifference) > 1 Then
        lastCell = x - 1
        Range("B" & (x)).EntireRow.Insert
        Range("A" & (x)) = "StdDev"
        For y = 2 To lastColumn
            Cells(x, y).Select
        Next y
        x = x + 1
        firstCell = x
        initialValue = initialValue + stepSize
    End If
Next x

我试图使用下面的代码:

Cells(x, y) = "=STDEV.P(" & Cells(firstCell, y) & ":" & Cells(lastCell, y) & ")"

但这给出了错误,“应用程序定义的或对象定义的错误”。我需要知道如何将单元格参考值输入到公式中,但让它成为一个依赖于循环的动态范围。

对此的任何帮助将不胜感激。谢谢你。

4

1 回答 1

1

尝试这样的事情:

            Cells(x, y).FormulaR1C1 = "=STDEVP(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")"
于 2013-10-16T03:00:10.930 回答