应用程序定义或对象定义的函数错误

时间:2018-09-18 15:18:09

标签: excel vba excel-vba ms-error-1004

我遇到了运行时错误1004应用程序定义的错误或对象定义的错误,我更新了以下功能以包括“访问”代码。我似乎找不到我的问题所在。有人有任何输入吗?我已经老了,我需要输入更多详细信息,但是我不确定应该写些什么,所以这里有一些填充物?

Dim Access As Worksheet
Dim report As Worksheet
Dim complete As Worksheet

Sub finalOverlay()

    Application.ScreenUpdating = False

    Set Access = ActiveWorkbook.Worksheets("Access")
    Set report = ActiveWorkbook.Worksheets("Report")
    Set complete = ActiveWorkbook.Worksheets("Implementation Complete")

    Dim i As Long

    For x = 2 To Access.UsedRange.Rows.Count
        For i = 2 To report.UsedRange.Rows.Count


            getRemainingControls (i)

            ' update completed list if no more actions outstanding
            If report.Cells(i, 17) = 0 Then                    
                 t = setComplete(report.Cells(i, 1), i)
            End If

            S = getComplete(report.Cells(i, 1), i)

            ' Add BAU Ageing
            report.Cells(i, 22) = BAUAgeing(i)

            ' Disposition Date past
            If report.Cells(i, 34) <> "" Then
                If CDate(report.Cells(i, 34)) < Date Then
                    report.Cells(i, 34).Interior.ColorIndex = 3
                End If
            End If


            ' Proposed Status for too long
            If report.Cells(i, 4) = "Proposed" Then
                report.Cells(i, 18) = CInt(Date - CDate(report.Cells(i, 38)))
                If report.Cells(i, 18) > 30 Then
                    report.Cells(i, 18).Interior.ColorIndex = 3
               End If
            End If


        Next i
    Next x

    For n = 2 To report.UsedRange.Rows.Count
        If report.Cells(n, 19) <> " " And _
           report.Cells(n, 19) > 305 And _
           report.Cells(n, 23) = "Yes" And _
           report.Cells(n, 3) <> "LOW" And _
           report.Cells(n, 39) <> "NO" Then
              report.Cells(n, 19).Interior.ColorIndex = 45
        End If

        If report.Cells(n, 19) <> " " And _
           report.Cells(n, 19) > 60 And _
           report.Cells(n, 24) <> "Complete" And _
           report.Cells(n, 24) <> "" And _
           report.Cells(n, 23) = "Yes" And _
           report.Cells(n, 3) <> "LOW" And _
           report.Cells(n, 39) <> "NO" Then
               report.Cells(n, 19).Interior.ColorIndex = 3
        End If

        If report.Cells(n, 20) > 305 And _
           report.Cells(n, 20) < 364 And _
           report.Cells(n, 29) = "Complete" And _
           report.Cells(n, 23) = "Yes" And _
           report.Cells(n, 3) <> "LOW" And _
           report.Cells(n, 39) <> "NO" Then
               report.Cells(n, 20).Interior.ColorIndex = 45
        End If

        If report.Cells(n, 20) > 60 And _
           report.Cells(n, 29) <> "Complete" And _
           report.Cells(n, 23) = "Yes" And _
           report.Cells(n, 3) <> "LOW" And _
           report.Cells(n, 39) <> "NO" Then
               report.Cells(n, 20).Interior.ColorIndex = 3
        End If

    Next n

    Application.ScreenUpdating = True

End Sub

Function BAUAgeing(i As Long)

    If report.Cells(i, 17) > 0 And _
       report.Cells(i, 6) <> "" And _
       report.Cells(i, 23) = "No" Then
           BAUAgeing = Date - CDate(report.Cells(i, 6))

    Else
    End If

    If report.Cells(i, 4) = "Newly Disclosed" And Access.Cells(x, 35) = "" Then
        BAUAgeing = Date - CDate(report.Cells(i, 38))
    Else
        BAUAgeing = Date - CDate(Mid(Access.Cells(x, 35), 16, 10))
    End If

    If report.Cells(i, 4) = "Proposed" And Access.Cells(x, 35) = "" Then
        BAUAgeing = Date - CDate(report.Cells(i, 38))
    Else
        BAUAgeing = Date - CDate(Mid(Access.Cells(x, 35), 16, 10))
    End If

End Function

1 个答案:

答案 0 :(得分:0)

您需要更改函数以使用x作为参数,否则它将没有任何值,并且会出现1004错误:

Function BAUAgeing(i As Long, x as long)

在上面的代码中,还要更改参数:

' Add BAU Ageing
report.Cells(i, 22) = BAUAgeing(i, x)

此外,您确实应该在模块顶部添加Option Explicit并声明变量的全部。您声明i,但没有声明txS ...留下了很多犯错的空间。