简体   繁体   中英

Excel vba error 1004 - insert a formula

i'm trying to execute these code in my excel sheet

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

and i'm get an #1004 error with no more informations. Can anybody eyplain my failure? I hav some others formulars insert in the same way...thx

EDIT: My Tables look like that 在此处输入图片说明

This should be a projectmanagement tool - Breitband Delphi Method ;) So my code goes through all the rows and check in which column the descripton is (level 1,2,3,4). Next the code is adding the rows 8-12 for example.. here i can enter some informations for the project... and now my script should add the formula at column kn.

My code is not very nice (as my english :) ) - it is just a prototype..

This is my Loop

   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

Original formula (MS standard) uses " , " instead of " ; "

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

or use:

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

Please, refer this:
Formula
FormulaLocal

[EDIT]

First of all...
IsEmpty indicates whether a variable (of variant) has been initialized. So, if you want to check if cell is empty (does not contains any value), use:

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

Second of all..
Your code has no context. What it means? Using ActiveCell or Range("") or Cell() depends on what workbook (and its sheet) is actually in usage! You should use code in context:

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

Third of all...
Review and debug you code and start again using above tips ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM