Excel宏运行时错误1004

时间:2016-06-06 04:26:54

标签: excel vba excel-vba macros runtime-error

当我运行它时,我想知道这个宏导致运行时错误1004的原因是什么:

    Sub brand()
'
' brand Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "Brand"
    Range("M2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(ISNUMBER(SEARCH(""trident"", RC[-12]))), ""Trident"", IF(OR(ISNUMBER(SEARCH(""stride"", RC[-12]))),""Stride"", IF(OR(ISNUMBER(SEARCH(""mints"", RC[-12]))), ""Mints"", IF(OR(ISNUMBER(SEARCH(""gum bubb"", RC[-12]))), ""Gum Bubb"", IF(OR(ISNUMBER(SEARCH(""gum heritage"", RC[-12]))), ""Gum Heritage"", IF(OR(ISNUMBER(SEARCH(""dentyne"", RC[-12]))), ""Dentyne"", IF" & _
        "MBER(SEARCH(""candy struble"", RC[-12]))), ""Candy Struble"", IF(OR(ISNUMBER(SEARCH(""halls struble"", RC[-12]))), ""Halls"", ""Other""))))))))"
    Range("M2").Select
    Selection.AutoFill Destination:=Range("M2:M525998")
    Range("M2:M525998").Select
    Range("O8").Select
End Sub

我有一个类似的宏,它实际上在我运行宏时起作用了:

    Sub location()
'
' location Macro
' fill in location
'
' Keyboard Shortcut: Ctrl+Shift+L
'
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "Location"
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        " =IF(OR(ISNUMBER(SEARCH(""ontario"",G4))),""Ontario"", IF(OR(ISNUMBER(SEARCH(""carlisle"",G4))),""Carlisle"",IF(OR(ISNUMBER(SEARCH(""orchard"",G4))),""Orchard"",""Other"")))                                                                                                     "
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(ISNUMBER(SEARCH(""ontario"",R[2]C[-5]))),""Ontario"",IF(OR(ISNUMBER(SEARCH(""carlisle"",R[2]C[-5]))),""Carlisle"",IF(OR(ISNUMBER(SEARCH(""orchard"",R[2]C[-5]))),""Orchard"",""Other"")))"
    Range("L2").Select
    Selection.AutoFill Destination:=Range("L2:L425682")
    Range("L2:L425682").Select
    Range("N3").Select
End Sub

这两个宏都是通过录制它们创建的,并且两者都做了相同的过程(品牌宏有一个更大的公式)。但由于某种原因,品牌宏有运行时错误,而位置宏则没有。我是excel的新手所以我想知道是否有人知道如何解决它。谢谢。

1 个答案:

答案 0 :(得分:1)

当你将包含公式的长字符串分成两行时,你丢失了几个字符。

Sub brand()
' brand Macro
' Keyboard Shortcut: Ctrl+Shift+B

    With Worksheets("Sheet3")   'you should know what worksheet you are planning to put a formula on
        .Range("M1") = "Brand"
        'as an xlA1 style formula
        .Range("M2:M525998").Formula = _
            "=IF(OR(ISNUMBER(SEARCH(""trident"", RC[-12]))), ""Trident"", " & _
             "IF(OR(ISNUMBER(SEARCH(""stride"", RC[-12]))), ""Stride"", " & _
             "IF(OR(ISNUMBER(SEARCH(""mints"", RC[-12]))), ""Mints"", " & _
             "IF(OR(ISNUMBER(SEARCH(""gum bubb"", RC[-12]))), ""Gum Bubb"", " & _
             "IF(OR(ISNUMBER(SEARCH(""gum heritage"", RC[-12]))), ""Gum Heritage"", " & _
             "IF(OR(ISNUMBER(SEARCH(""dentyne"", RC[-12]))), ""Dentyne"", " & _
             "IF(OR(ISNUMBER(SEARCH(""candy struble"", RC[-12]))), ""Candy Struble"", " & _
             "IF(OR(ISNUMBER(SEARCH(""halls struble"", RC[-12]))), ""Halls"", ""Other""))))))))"
        'or as an xlR1C1 style formula
        '.Range("M2:M525998").FormulaR1C1 = _
            "=IF(OR(ISNUMBER(SEARCH(""trident"", RC[-12]))), ""Trident"", " & _
             "IF(OR(ISNUMBER(SEARCH(""stride"", RC[-12]))), ""Stride"", " & _
             "IF(OR(ISNUMBER(SEARCH(""mints"", RC[-12]))), ""Mints"", " & _
             "IF(OR(ISNUMBER(SEARCH(""gum bubb"", RC[-12]))), ""Gum Bubb"", " & _
             "IF(OR(ISNUMBER(SEARCH(""gum heritage"", RC[-12]))), ""Gum Heritage"", " & _
             "IF(OR(ISNUMBER(SEARCH(""dentyne"", RC[-12]))), ""Dentyne"", " & _
             "IF(OR(ISNUMBER(SEARCH(""candy struble"", RC[-12]))), ""Candy Struble"", " & _
             "IF(OR(ISNUMBER(SEARCH(""halls struble"", RC[-12]))), ""Halls"", ""Other""))))))))"
        .Range("O8").Select
    End With
End Sub

可以立即将公式写入所有单元格;无需播种第一个单元格,然后使用Range.AutoFill method

我把公式分成了很多行;我更喜欢在VBE窗口中看到尽可能多的有效代码。

我担心你已经硬编码了最后一行。必须有一个其他的列,你可以用

之类的东西收集最后一行
dim lrw as long
lrw = .cells(rows.count, "A").end(xlup).row

这允许您指定接收公式的行数。

.Range("M2:M" & lrw).Formula = _

tbh,我不完全理解你为什么在那个公式中有OR function