VBA - 错误1004

时间:2016-04-11 20:43:32

标签: vba function match

此行显示运行时错误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

2 个答案:

答案 0 :(得分: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

请参阅here,了解如何避免使用.Select及其原因。

答案 1 :(得分: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

进行了两项修改:

  1. 使用FormulaR1C1代替Formula
  2. 替换;
  3. 的公式中的,