2015-01-15 138 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