簡體   English   中英

Excel VBA錯誤1004-插入公式

[英]Excel vba error 1004 - insert a formula

我正在嘗試在我的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

編輯:我的表看起來像那樣 在此處輸入圖片說明

這應該是一個項目管理工具-Breitband Delphi Method;)因此,我的代碼遍歷了所有行,並檢查描述符位於哪一列(1,2,3,4級)。 接下來,代碼將添加第8至12行,例如..在這里,我可以為項目輸入一些信息...現在,我的腳本應在第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指示變量(變量)是否已初始化。 因此,如果要檢查單元格是否為空(不包含任何值),請使用:

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

第二..
您的代碼沒有上下文。 這是什么意思? 使用ActiveCellRange(“”)Cell()取決於實際使用的工作簿(及其工作表)! 您應該在上下文中使用代碼:

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

第三...
查看並調試您的代碼,然后使用上述提示重新開始;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM