2016-04-11 169 views
1

此行顯示了運行時錯誤1004,但我不明白爲什麼:VBA - 錯誤1004

ActiveCell.Formula = "=MATCH(R" & i & "C16;R" & i & "C6:R" & i & "C15;0)" 
Sub Call_Min() 

    Dim i As Integer 
    Dim limit As Integer 
    Sheets("AUX").Activate 
    limit = ActiveSheet.Range("B6").Value 

    Sheets("DATA").Activate 
    'ActiveSheet.Cells(6, 16).Select 
    'ActiveCell.Formula = "=SUM(Range("F6:I6"))" 
    For i = 6 To limit 

     'MATCH(P6;F6:O6;0) 
     ActiveSheet.Range("P" & i).Select 
     ActiveCell.Formula = "=MIN(R" & i & "C6:R" & i & "C15)" 

     ActiveSheet.Range("E" & i).Select 
     ActiveCell.Formula = "=MATCH(R" & i & "C16;R" & i & "C6:R" & i & "C15;0)" 
    Next i 

End Sub 
+1

它應該是'ActiveCell.FormulaR1C1'而不是'ActiveCell.Formula'。 – Ralph

+1

將'ActiveCell.Formula'改爲'ActiveCell.FormulaR1C1Local' –

回答

2

的問題是,VBA是很美式英語中心。因此,在地方的;要麼使用,或使用.FormulaR1C1Local

Sub Call_Min() 
Dim i As Integer 
Dim limit As Integer 

limit = Sheets("AUX").Range("B6").Value 

Sheets("Sheet11").Activate 
'ActiveSheet.Cells(6, 16).Select 
'ActiveCell.Formula = "=SUM(Range("F6:I6"))" 
For i = 6 To limit 

     'MATCH(P6;F6:O6;0) 
     Sheets("DATA").Range("P" & i).FormulaR1C1Local = "=MIN(R" & i & "C6:R" & i & "C15)" 

     Sheets("DATA").Range("E" & i).FormulaR1C1Local = "=MATCH(R" & i & "C16;R" & i & "C6:R" & i & "C15;0)" 
Next i 
End Sub 

參見如何避免使用.Select爲什麼here

+0

++以避免選擇和ActiveCell –

1
Sub Call_Min() 

    Dim i As Integer 
    Dim limit As Integer 
    Sheets("AUX").Activate 
    limit = ActiveSheet.Range("B6").Value 

    Sheets("DATA").Activate 
    'ActiveSheet.Cells(6, 16).Select 
    'ActiveCell.Formula = "=SUM(Range("F6:I6"))" 
    For i = 6 To limit 

     'MATCH(P6;F6:O6;0) 
     ActiveSheet.Range("P" & i).Select 
     ActiveCell.FormulaR1C1 = "=MIN(R" & i & "C6:R" & i & "C15)" 

     ActiveSheet.Range("E" & i).Select 
     ActiveCell.FormulaR1C1 = "=MATCH(R" & i & "C16,R" & i & "C6:R" & i & "C15,0)" 
    Next i 

End Sub 

兩個改變:

用法 FormulaR1C1代替 Formula
  1. ,的公式中替換;