2015-01-15 137 views
0

我想要在我的excel表Excel中VBA错误1004 - 插入公式

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

执行这些代码,我得到一个#1004错误,没有更多的信息。任何人都可以解释我的失败吗? 我甲肝一些人formulars以同样的方式插入... THX

编辑: 我的表看起来像 enter image description here

这应该是一个项目管理工具 - Breitband德尔菲法;) 所以我的代码去通过所有行并检查描述符在哪一列(级别1,2,3,4)。 接下来的代码是添加行8-12例如..在这里我可以输入项目的一些信息...现在我的脚本应该添加公式在列k-n。

我的代码是不是很好(我的英文:)) - 它只是一个原型..

这是我的循环

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 
+3

你试过','S表示',' S' – pnuts

+0

是的,我从我的示例工作表中复制了公式,并添加了变量。 – Tobias

+0

'i'是如何定义的? – Chrismas007

回答

1

原配方(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)" 

请参阅此:
Formula
FormulaLocal

[编辑]

首先,...
IsEmpty指示是否(变种)的变量已经被初始化。所以,如果你想检查是否细胞是空的(不包含任何值),用途:

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

所有第二..
您的代码没有上下文。这是什么意思?使用ActiveCell范围(“”)单元()取决于实际使用的工作簿(及其工作表)。 你应该在上下文中使用代码:

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

所有的三...
审查和调试你的代码,并开始再次使用上面的提示;)

+1

这是正确的答案,它为什么被downvoted?我复制@Tobias错误与“;”和Maciej Los写的代码一样完美。请告诉我们什么是行不通的。 –

+0

我不知道谁降低了他的...我已经尝试了两种解决方案,但我得到了相同的错误代码..“1004”我没有关于该错误的更多信息 – Tobias

+0

Tobias表示第一个版本没有解决他的问题问题,如果第一个版本没有机会看起来不好,第二个版本会。这可能是一个很好的答案,但似乎不是这个问题。 – pnuts