宏不运行公式吗?

时间:2019-01-31 09:32:43

标签: excel vba

脚本按预期执行Test(),但是runFormula()引发以下错误。

  

错误:运行时错误1004应用程序定义或对象定义错误

此功能基本上使用R1C1表示法来检查某些单元格中是否包含信息。

Sub Test()  
    ' Test Macro  
    ' Filters a column and inserts two blank columns  

    ActiveSheet.Range("$A$1:$BG$3877").AutoFilter Field:=11, Criteria1:=Array( _  
        "Awaiting Fit and Test", "Awaiting Hardware Install", "Awaiting   Install/Fibre", _  
        "Awaiting IP Allocation", "Awaiting Projects", "Awaiting Survey Date", _  
        "Awaiting Survey Results", "Awaiting Task Closure", "Not Started", _  
        "On Hold - Excess Charges", "On Hold - Other Circuits", "On Hold - With Customer" _  
        , "On Hold - With Projects", "Place Order", "Wayleave"), Operator:= _
        xlFilterValues
    ActiveWindow.ScrollColumn = 2  
    ActiveWindow.ScrollColumn = 3  
    ActiveWindow.ScrollColumn = 4  
    ActiveWindow.ScrollColumn = 5  
    ActiveWindow.ScrollColumn = 6  
    ActiveWindow.ScrollColumn = 7  
    ActiveWindow.ScrollColumn = 8  
    ActiveWindow.ScrollColumn = 9  
    ActiveWindow.ScrollColumn = 10  
    ActiveWindow.ScrollColumn = 11  
    ActiveWindow.ScrollColumn = 12  
    ActiveWindow.ScrollColumn = 14  
    ActiveWindow.ScrollColumn = 15  
    ActiveWindow.ScrollColumn = 16  
    ActiveWindow.ScrollColumn = 17  
    ActiveWindow.ScrollColumn = 18  
    ActiveWindow.ScrollColumn = 19  
    ActiveWindow.ScrollColumn = 20  
    ActiveWindow.ScrollColumn = 21  
    ActiveWindow.ScrollColumn = 22  
    ActiveWindow.ScrollColumn = 23  
    ActiveWindow.ScrollColumn = 24  
    ActiveWindow.ScrollColumn = 25  
    ActiveWindow.ScrollColumn = 26  
    ActiveWindow.ScrollColumn = 27  
    ActiveWindow.ScrollColumn = 28  
    ActiveWindow.ScrollColumn = 29  
    ActiveWindow.ScrollColumn = 30  
    ActiveWindow.ScrollColumn = 31  
    ActiveWindow.ScrollColumn = 32  
    ActiveWindow.ScrollColumn = 33  
    ActiveWindow.ScrollColumn = 30  
    ActiveWindow.ScrollColumn = 24  
    ActiveWindow.ScrollColumn = 20  
    ActiveWindow.ScrollColumn = 19  
    ActiveWindow.ScrollColumn = 20  
    Columns("AL:AL").Select  
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove  
    Range("AL1").Select  
    ActiveCell.FormulaR1C1 = "Stg1 expected & sent"  
    Columns("AM:AM").Select  
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove  
    Range("AM1").Select  
    ActiveCell.FormulaR1C1 = "Stg1 not expected & sent"  
    Range("AM6").Select  
    Columns("AL:AL").EntireColumn.AutoFit  
    Columns("AM:AM").EntireColumn.AutoFit  

    runFormula
End Sub


Sub runFormula()
    Sheets("Milestone Report").Range("AL6").FormulaR1C1 = "=IF(AND(OR(AND(R[0]C[-32]=""PSTN"", R[0]C[-3]="""", R[0]C[-2]="""",=""""), AND(R[0]C[-32]=""DSL"",NOT(ISBLANK(R[0]C[-3])), R[0]C[-2]=""""),AND(R[0]C[-32]=""Ethernet"",NOT(ISBLANK(R[0]C[-3])),NOT(ISBLANK(R[0]C[-2])))),NOT(ISBLANK(R[0]C[-2]))),""YES"",""NO"")"
End Sub

2 个答案:

答案 0 :(得分:1)

您缺少单个单元格引用,但我不知道该放在哪里。

=IF(AND(OR(AND(R[0]C[-32]=""PSTN"", R[0]C[-3]="""", R[0]C[-2]="""",=""""), ...

最后,您自己拥有="""";应该类似于R[0]C[-1]=""""

xlA1中的前两个是AI6=""AJ6=""

这是您的代码收紧了一点。该公式尚未确定。顺便说一句,如果您也想清理它们,R [0] C [-2]与RC [-2]相同。

Option Explicit

Sub Test()
' Filters a column and inserts two blank columns

    Dim arr As Variant

    arr = Array("Awaiting Fit and Test", "Awaiting Hardware Install", "Awaiting Install/Fibre", _
                "Awaiting IP Allocation", "Awaiting Projects", "Awaiting Survey Date", _
                "Awaiting Survey Results", "Awaiting Task Closure", "Not Started", _
                "On Hold - Excess Charges", "On Hold - Other Circuits", "On Hold - With Customer", _
                "On Hold - With Projects", "Place Order", "Wayleave")

    With Worksheets("Milestone Report")

        '.Range("A1:BG3877").AutoFilter Field:=11, Criteria1:=arr, Operator:=  xlFilterValues

        .Columns("AL:AL").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range("AL1") = "Stg1 expected & sent"
        .Columns("AM:AM").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range("AM1") = "Stg1 not expected & sent"

        .Columns("AL:AL").EntireColumn.AutoFit
        .Columns("AM:AM").EntireColumn.AutoFit

    End With

    runFormula

End Sub

Sub runFormula()

    Worksheets("Milestone Report").Range("AL6").FormulaR1C1 = _
      "=IF(AND(OR(AND(RC[-32]=""PSTN"", RC[-3]="""", RC[-2]="""", =""""), AND(RC[-32]=""DSL"", NOT(ISBLANK(RC[-3])), RC[-2]=""""), AND(RC[-32]=""Ethernet"", NOT(ISBLANK(RC[-3])), NOT(ISBLANK(RC[-2])))), NOT(ISBLANK(RC[-2]))), ""YES"", ""NO"")"

End Sub

答案 1 :(得分:0)

一般提示:

如果添加新公式,请在工作表中手动构建。
一旦功能齐全,请使用宏记录器将其记录下来,例如。 G。通过在该公式中添加和删除一个字符即可。

如果您想重新检查相对的R1C1公式,则可以出于测试目的将其转换为采用绝对A1表示法的示例公式(仅当R1C1公式本身没有语法错误时才有效):

Dim myformula As String
myformula = "=IF(...)" ' your formula In R1C1 notation
Debug.Print Application.ConvertFormula( _
    Formula:=myformula, _
    FromReferenceStyle:=xlR1C1, _
    ToReferenceStyle:=xlA1, _
    ToAbsolute:=xlRelative, _
    RelativeTo:=Sheets("Milestone Report").Range("AL6"))

您的示例(如前所述,我删除了=

“ = IF(AND(OR(AND(R [0] C [-32] =”“ PSTN”“,R [0] C [-3] =”“”“,R [0] C [ -2] =“”“”,“”“”),AND(R [0] C [-32] =“” DSL“”,NOT(ISBLANK(R [0] C [-3])),R [0] C [-2] =“”“”),AND(R [0] C [-32] =“”以太网“”,否(ISBLANK(R [0] C [-3])),否(ISBLANK(R [0] C [-2])))),NOT(ISBLANK(R [0] C [-2]))),“” YES“”,“” NO“”)“

出于测试目的会导致以下结果:

= IF(AND(OR(AND(AND(F6 =“ PSTN”,AI6 =“”,AJ6 =“”,“”),AND(F6 =“ DSL”,NOT(ISBLANK(AI6)),AJ6 = “”),AND(F6 =“ Ethernet”,NOT(ISBLANK(AI6))),NOT(ISBLANK(AJ6)))),NOT(ISBLANK(AJ6))),“ YES”,“ NO”)