Excel VBA错误1004 – 插入一个公式

我试图在我的Excel表中执行这些代码

ActiveCell.Offset(0, 3).Formula = "=if(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)" 

我得到了#1004错误,没有更多的信息。 有人能够解释我的失败吗? 我有一些其他的公式插入相同的方式… thx

编辑:我的表看起来像那样 在这里输入图像说明

这应该是项目pipe理工具 – Breitband Delphi方法;)所以我的代码遍历所有的行,并检查描述符是哪一列(级别1,2,3,4)。 接下来的代码是添加行8-12例如..在这里我可以input一些信息的项目…现在我的脚本应该添加公式列kn。

我的代码不是很好(因为我的英文:)) – 这只是一个原型..

这是我的循环

  i = 5 canSkip = False Do ' fist first the level If Not IsEmpty(Range("B" & i).Value) Then level = 1 If Not IsEmpty(Range("D" & i + 1)) Then ' ye we can - so skip this loop canSkip = True End If ElseIf Not IsEmpty(Range("D" & i).Value) Then level = 2 If Not IsEmpty(Range("F" & i + 1)) Then ' ye we can - so skip this loop canSkip = True End If ElseIf Not IsEmpty(Range("F" & i).Value) Then level = 3 If Not IsEmpty(Range("H" & i + 1)) Then ' ye we can - so skip this loop canSkip = True End If ElseIf Not IsEmpty(Range("H" & i).Value) Then level = 4 canSkip = False End If If canSkip = True Then i = i + 1 Else ' First insert some... and bang it to a group ' Insert Formula Range("K" & i).Activate ActiveCell.Formula = "=min(L" & i + 2 & ":L" & i + 5 & ")" ActiveCell.Offset(0, 1).Formula = "=max(L" & i + 2 & ":L" & i + 5 & ")" 'Range("T1").FormulaLocal = insertMedianFormula 'ActiveCell.Offset(0, 3).Formula = "=WENN(SUMME(N" & i + 2 & ":N" & i + 5 & ")>0;MITTELWERT(N" & i + 2 & ":N" & i + 5 & ");0)" Range("A" & i + 1).Activate For x = 1 To 5 ActiveCell.EntireRow.Insert If x = 5 Then If level = 1 Then ActiveCell.Offset(0, 1).Value = "Experte" ActiveCell.Offset(0, 2).Value = "Aufw." ActiveCell.Offset(0, 3).Value = "Bemerkung" ElseIf level = 2 Then ActiveCell.Offset(0, 3).Value = "Experte" ActiveCell.Offset(0, 4).Value = "Aufw." ActiveCell.Offset(0, 5).Value = "Bemerkung" ElseIf level = 3 Then ActiveCell.Offset(0, 5).Value = "Experte" ActiveCell.Offset(0, 6).Value = "Aufw." ActiveCell.Offset(0, 7).Value = "Bemerkung" ElseIf level = 4 Then ActiveCell.Offset(0, 7).Value = "Experte" ActiveCell.Offset(0, 8).Value = "Aufw." ActiveCell.Offset(0, 9).Value = "Bemerkung" End If ' now just bang it to a group ActiveCell.Resize(5, 10).Rows.Group End If Next x i = i + 6 End If ' are we finshed? If i > lastUsedRow Then Exit Do End If canSkip = False Loop 

原始公式(MS标准)使用“ ”而不是“ ;

 ActiveCell.Offset(0, 3).Formula = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0,MEDIAN(N" & i + 2 & ":N" & i + 5 & "),0)" 

或使用:

 ActiveCell.Offset(0, 3).FormulaLocal = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)" 

请参考这个:

FormulaLocal

[编辑]

首先…
IsEmpty表示variables(变体)是否已被初始化。 所以,如果你想检查单元格是否为空(不包含任何值),请使用:

 Range("B" & i)<>"" 

其次..
你的代码没有上下文。 这是什么意思? 使用ActiveCellRange(“”)Cell()取决于实际使用的工作簿(及其工作表)。 你应该在上下文中使用代码:

 With ThisWorkbook.Worksheets("SheetName") .Range("A1").Offset(0,i).Formula = "='Hello Kitty'" .Cell(2,i) = "123.45" End With 

第三个…
查看和debugging你的代码,并使用上面的提示重新开始;)