2014-11-01 71 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行