输入公式时出现运行时错误“1004”

时间:2014-11-01 15:44:49

标签: excel vba excel-vba runtime-error excel-vba-mac

在excel 2011 for mac上运行时,我得到的错误是:

  

运行时错误'1004':
  Microsoft Excel找不到要替换的匹配数据。选择中的任何单元格都不包含您键入的内容,或者没有符合条件的记录。

奇怪的是程序运行并且公式在我希望的单元格中正确输入。

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

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

你知道造成这个错误的原因以及如何摆脱它吗?

1 个答案:

答案 0 :(得分:1)

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