vba - Excel 宏运行时错误 1004

标签 vba excel 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 新手,所以我想知道是否有人知道如何解决它。谢谢你。

最佳答案

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

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在那个公式中。

关于vba - Excel 宏运行时错误 1004,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649537/

相关文章:

c - 未使用的宏警告

vba - 使用宏重置单元格中的公式

Excel CurrentProject.Path,运行时错误 424

vba - Excel VBA 自毁开关

mysql - 将 Excel 表导出到关系表 MySQL

python - 在 Excel 工作簿上保存多个数据帧,然后上传到 AWS S3 存储桶

clojure - 为什么 Clojure 试图解析这个符号?

vba - 在 VBA 中可以实现真正的 ByRef 吗?

vba - 如何在VBA中调用另一个特定工作簿的函数?

excel - 如何在 Excel 2016 VBA 中读取已用或可用内存量