CountIf应用程序或对象定义错误

时间:2017-06-01 12:10:47

标签: excel vba excel-vba

我有一个代码继续返回运行时错误1004 - 应用程序定义或对象定义的错误。我试过单步执行worksheetfunction.countif函数的各个部分,它们都可以单独工作。

然而,当我把它们放在一起时,它们会失败。

代码是:

s = 2

While Cells(s - 1, 1) <> vbNullString

    Rows(s & ":" & s + 3).Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range(Cells(s, 1), Cells(s + 3, 1)).Select
    Selection.Rows.Group
    Cells(s, 1) = "A"
    Cells(s + 1, 1) = "B"
    Cells(s + 2, 1) = "C"
    Cells(s + 3, 1) = "D"

    r = 3
    q = vbNullString
    p = vbNullString
    n = s

    While n < s + 5
        While r <= v
            M = 1
            If Cells(n, 1) = "A" Then
                q = 5
                p = 12
            ElseIf Cells(n, 1) = "B" Then
                q = 18
                p = 25
            ElseIf Cells(n, 1) = "C" Then
                q = 31
                p = 38
            ElseIf Cells(n, 1) = "D" Then
                q = 44
                p = 51
            End If

            While M <= u
                l = vbNullString
                l = WorksheetFunction.CountIf(Worksheets("IT Teams").Range(Cells(q, M), Cells(p, M)), Worksheets("Players IT").Cells(s + 4, 1))
                If Not IsError(l) Then
                    Cells(n, r) = l
                Else
                    Cells(n, r) = vbNullString
                End If
                M = M + 5
                r = r + 1
            Wend

        Wend
        n = n + 1
        r = 3
    Wend

    s = s + 5
Wend

所有变量都已声明为Variants。

编辑:为清楚起见。错误发生在:

l = WorksheetFunction.CountIf(Worksheets("IT Teams").Range(Cells(q, M), Cells(p, M)), Worksheets("Players IT").Cells(s + 4, 1))

1 个答案:

答案 0 :(得分:2)

问题在于您声明范围的方式。您应该始终包含工作表,否则会出现此错误,如果您使用多个工作表(或者如果您使用一个工作表,但它不是活动工作表)。

像这样:

With ActiveSheet
    While Cells(s - 1, 1) <> vbNullString

        .Rows(s & ":" & s + 3).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range(.Cells(s, 1), .Cells(s + 3, 1)).Select
        Selection.Rows.Group
        .Cells(s, 1) = "A"
        .Cells(s + 1, 1) = "B"
        .Cells(s + 2, 1) = "C"
        .Cells(s + 3, 1) = "D"
    Wend
End With

注意点。

通常,声明工作表然后使用它们:

'Option Explicit - start using option explicit
Sub test()

    Dim wksA        As Worksheet
    Dim wksIT       As Worksheet

    Set wksA = ThisWorkbook.ActiveSheet
    Set wksIT = ThisWorkbook.Worksheets("IT Teams")

    s = 2
    While Cells(s - 1, 1) <> vbNullString

        wksA.Rows(s & ":" & s + 3).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        wksA.Range(wksA.Cells(s, 1), wksA.Cells(s + 3, 1)).Select
        Selection.Rows.Group
        wksA.Cells(s, 1) = "A"
        wksA.Cells(s + 1, 1) = "B"
        wksA.Cells(s + 2, 1) = "C"
        wksA.Cells(s + 3, 1) = "D"
    Wend

    With wksIT

        While M <= u
            l = vbNullString
            l = WorksheetFunction.CountIf(.Range(.Cells(q, M), _
                                       .Cells(p, M)), .Cells(s + 4, 1))
                If Not IsError(l) Then

                .Cells(n, r) = l
            Else
                .Cells(n, r) = vbNullString
            End If
            M = M + 5
            r = r + 1
        Wend
    End With

End Sub

关于你的情况,我大约80%肯定,你在这里得到了错误:

    l = WorksheetFunction.CountIf(Range(Cells(q, M), Cells(p, M)), Cells(s + 4, 1))

通常,永远不要假设您的代码在哪个工作表上运行,并在代码中明确定义它。

关于您收到错误的地方,应该是这样的:

    Set wksA = ThisWorkbook.ActiveSheet
    Set wksIT = ThisWorkbook.Worksheets("IT Teams")
    Set wksPl = ThisWorkbook.Worksheets("SomePlayers")

    l = WorksheetFunction.CountIf(wksIT.Range(wksIT.Cells(q, M), wksIT.Cells(p, M)), _
    wksPl.Cells(s + 4, 1))