2014-11-01 70 views
1

当运行这个于2011年的Excel的Mac,我得到这个错误是:运行时错误“1004”,同时输入公式

运行时错误“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 

你知道是什么原因造成这个错误,我如何能摆脱它?

+0

上传完整的代码,包括关于rowNum,rowsToSum,CellstoSum – ZAT 2014-11-01 16:00:58

+0

代码上传.. – mks212 2014-11-01 16:05:26

+0

调试时,该行被突出显示,'cellsToSum'的值是什么?它还是0吗? – 2014-11-01 17:50:44

回答

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行