对象_Global的方法“范围”失败

时间:2016-06-02 17:30:19

标签: excel vba excel-vba

这个问题已被提出,但我无法找到答案。我知道当以某种方式未指定Range时会发生此错误。但是,我已经指定了范围,或者我认为我做了它并且它不想工作。

该代码旨在从编码的记录中创建两个新的工作表,然后添加不同的公式以两种不同的方式对代码进行计数。 “LastRow”代码适用于第一张表,但不适用于第二张表,我无法弄清楚为什么或如何解决它。

我感谢任何建议。

以下是代码:

    Sub FullDurationCoding()
    ' Format the file to run the Duration Codes on two new worksheets.
      ActiveWorkbook.Worksheets(1).Name = "Transcript"
      Sheets("Transcript").Select
        Sheets("Transcript").Copy After:=Sheets("Transcript")
          ActiveSheet.Name = "TopicDuration"
      Sheets("Transcript").Select
        Sheets("Transcript").Copy After:=Sheets("TopicDuration")
          ActiveSheet.Name = "TurnDuration"

    ' Count number of lines till "@End"
       Dim LastRow As Single
       LastRow = Range("A" & Rows.Count).End(xlUp).Row

 ' Add TopicDuration formulas.
    ' Add Topic Duration Count formula to column C
        Sheets("TopicDuration").Select
        Range("C2:C" & LastRow).Formula = "=IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0""},B2))),1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:UNT"",""$TOP:SLF:SPL"",""$TOP:OTH:UNT"",""$TOP:OTH:OVR:SPL""},B2))),C1+0,IF(OR(ISNUMBER(SEARCH({""TOP:SLF:CON:0"",""TOP:SLF:CON:MIX"",""TOP:OTH:CON:0"",""TOP:OTH:CON:MIX"",""TOP:OTH:OVR:CON:0"",""TOP:OTH:OVR:CON:MIX"",""TOP:OTH:OVR:FLW:0"",""TOP:OTH:OVR:FLW:MIX""},B2))),C1+1,C1+0)))"
        Range("C1").Value = 1
    ' Add Topic Duration Calculation formula to column D
        Range("D2:D" & LastRow).Formula = "=IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0""},B2))),C1,""NO"")"
        Range("D1").Value = "Calculation"
    ' Add Subtotal Calulation to Cell E1 through E15
        Range("E1").Formula = "=SUBTOTAL(1,D:D)"
    ' Filter Column D.
        Range("A1:E" & LastRow).AutoFilter Field:=4, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>NO"

' Add TurnDurationFormulas()
    ' Add Turn Duration Count formula to column C
        Sheets("TurnDuration").Select
        Range("C1").Value = 1
        Range("C2").Formula = "=IF(A1=""CHI"",0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0"",""$TOP:OTH:CON:0"",""$TOP:OTH:CON:MIX"",""$TOP:OTH:OVR:CON:MIX"",""$TOP:OTH:OVR:CON:0""},B2))),1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:UNT"",""$TOP:SLF:SPL"",""$TOP:OTH:UNT"",""$TOP:OTH:OVR:SPL""},B2))),C1+0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:CON:0"",""$TOP:SLF:CON:MIX""},B2))),C1+1,IF(A2=""com"",C1+0,IF(A1=""cod"",C1+0,IF(A1=""MOT"",C1+1,C1+0)))))))"
        Range("C3").Formula = "=IF(A2=""CHI"",0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0"",""$TOP:OTH:CON:0"",""$TOP:OTH:CON:MIX"",""$TOP:OTH:OVR:CON:MIX"",""$TOP:OTH:OVR:CON:0""},B3))),1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:UNT"",""$TOP:SLF:SPL"",""$TOP:OTH:UNT"",""$TOP:OTH:OVR:SPL""},B3))),C2+0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:CON:0"",""$TOP:SLF:CON:MIX""},B3))),C2+1,IF(OR(ISNUMBER(SEARCH({""$TOP:OTH:OVR:FLW:0"",""$TOP:OTH:OVR:FLW:MIX""},B3))),C1+1,IF(A3=""com"",C2+0,IF(A2=""cod"",C2+0,IF(A2=""MOT"",C2+1,C2+0))))))))"
        Range("C4").Formula = "=IF(A3=""CHI"",0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0"",""$TOP:OTH:CON:0"",""$TOP:OTH:CON:MIX"",""$TOP:OTH:OVR:CON:MIX"",""$TOP:OTH:OVR:CON:0""},B4))),1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:UNT"",""$TOP:SLF:SPL"",""$TOP:OTH:UNT"",""$TOP:OTH:OVR:SPL""},B4))),C3+0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:CON:0"",""$TOP:SLF:CON:MIX""},B4))),C3+1,IF(OR(ISNUMBER(SEARCH({""$TOP:OTH:OVR:FLW:0"",""$TOP:OTH:OVR:FLW:MIX""},B4))),C1+1,IF(A4=""com"",C3+0,IF(A3=""cod"",C3+0,IF(A3=""MOT"",C3+1,C3+0))))))))"
 *****  Range("C5:C" & LastRow).Formula = "=IF(A4=""CHI"",0,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0"",""$TOP:OTH:CON:0"",""$TOP:OTH:CON:MIX"",""$TOP:OTH:OVR:CON:MIX"",""$TOP:OTH:OVR:CON:0""},B5))),1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:UNT"",""$TOP:SLF:SPL"",""$TOP:OTH:UNT"",""$TOP:OTH:OVR:SPL""},B5))),C4+0,IF(OR(""$TOP:SLF:CON:0"",""$TOP:SLF:CON:MIX""},B5))),C4+1,IF(OR(ISNUMBER(SEARCH({""$TOP:OTH:OVR:FLW:0"",""$TOP:OTH:OVR:FLW:MIX""},B5))),C1+1,IF(A5=""com"",C4+0,IF(A4=""cod"",C4+0,IF(A4=""MOT"",C4+1,C4+0))))))))"
    ' Add Turn Duration Calculation formula to column D
  ***   Range("D2:D" & LastRow).Formula = "=IF(C2=0,C1,IF(OR(ISNUMBER(SEARCH({""$TOP:SLF:NON:0"",""$TOP:SLF:NON:MIX"",""$TOP:OTH:NON:0"",""$TOP:OTH:NON:MIX"",""$TOP:OTH:OVR:NON:MIX"",""$TOP:OTH:OVR:NON:0""},B2))),C1,""NO"")"
        Range("D1").Value = "Calculation"
    ' Add Subtotal Calulation to Cell E1 through E15
        Range("E1").Formula = "=SUBTOTAL(1,D:D)"
    ' Filter Column D.
        Range("A1:E" & LastRow).AutoFilter Field:=4, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>NO"
    End Sub

具有*****的行是启动错误的行,但是当我通过调试系统时,带有***的行也会出错。我猜它必须与尝试在两个不同的工作表上定义Lastrow有关,但即使我将它们分成单独的Sub动作并将Dim代码放在第二个子中,它仍然会导致错误。这个代码工作得比较早,但是我需要稍微改变一下公式,一旦我做了,现在它就什么都不做了。 (但我知道这不是公式,因为我在前面的行和主题持续时间部分中以相同的方式修复了这个公式。)

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 为什么要将Dim LastRow声明为Single而不是Long?单可以存储十进制 - 无用 - 。
  2. LastRow无所事事,问题依赖于你的公式。
  3. 您正在使用数组公式
  4.   

    ... .Formula =“= IF(A4 =”“CHI”“,0,IF(OR(ISNUMBER)(搜索( { ..

    尝试更改以下行:.FormulaArray
    PS:不要自己输入字符{}。