繁体   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