input公式时运行时错误“1004”

虽然在excel 2011 for mac上运行这个,我得到这个错误是:

运行时错误“1004”:
Microsoft Excel找不到要replace的匹配数据。 选区中没有任何单元格包含您input的内容,或没有logging符合条件。

奇怪的是,程序运行,公式input正确的单元格,我希望它是。

点击debugging时突出显示的代码行是

ActiveSheet.Range("Q" & rowNum - 1).FormulaR1C1 = "=sum(R[-" & rowsToSum & "]C[-11]:RC[-11])/abs(sum(R[-" & cellsToSum & "]C[-13]:RC[-13]))/100" 

完整的循环与错误,

  For rowNum = 2 To lastRow + 1 'checks if cells A through O in a given row are empty If WorksheetFunction.CountA(Range(Cells(rowNum, 1), Cells(rowNum, 15))) = 0 Then rowsToSum = 0 'counts how many rows need to be added by checking if they are not blank. Current rowNum will be blank, so check one above and count back up While Application.Sum(Range(Cells((rowNum - rowsToSum - 1), 1), Cells((rowNum - rowsToSum - 1), 15))) <> 0 rowsToSum = rowsToSum + 1 Wend cellsToSum = 0 'Counts how many cells need to be added by checking if G/L column. Row above blank will have a value, rows above that need to be checked until non-blank is found While IsEmpty(Range("P" & rowNum - cellsToSum - 2)) cellsToSum = cellsToSum + 1 Wend rowsToSum = rowsToSum - 1 'needs to be decremented since it counted to the blank row above the current transaction Range("Q" & rowNum - 1).FormulaR1C1 = "=sum(R[-" & rowsToSum & "]C[-11]:RC[-11])/abs(sum(R[-" & cellsToSum & "]C[-13]:RC[-13]))/100" Range("R" & rowNum - 1).FormulaR1C1 = "=sum(R[-" & rowsToSum & "]C[-6]:RC[-6])/abs(sum(R[-" & cellsToSum & "]C[-14]:RC[-14]))/100" End If Next rowNum 

你知道是什么原因导致这个错误,我怎么能摆脱它?

For rowNum = 2 To lastRow + 1的第一个循环中, Range("Q" & rowNum - 1)单元格引用将等于Range("Q1") 。 如果增加rowsToSum = rowsToSum + 1的循环增加了rowsTosum (例如> 0),则在公式中使用rowsTosum sum(R[-" & rowsToSum & "]C[-11]:RC[-11])似乎是试图加总第1行以上的单元格引用。