函数错误,作为数组传递

时间:2013-09-05 22:48:02

标签: excel vba excel-vba excel-2010

我从sub调用一个函数但是它不起作用。我花了很多时间试图找出原因,但我只是没有看到它。我得到编译错误:预期的数组。

这是整个代码。我评论了有问题的一行

Sub innout()

Dim numclosed As Long, numnew As Long
Dim lblvar As Variant
Dim tblcr As Range
Dim lrcr As Long
Dim finishdates As Range
Dim lastupdate As Long, msg As Long



Sheets("combined_report").Select

    lrcr = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.count - 1, 0).End(xlUp).Row
    tblcr = Range("A6:AB" & lrcr)
    lastupdate = Range("F2").Value


    finishdates = Range("T6:T" & lrcr)

    For Each cell In finishdates

        If cell.Value >= lastupdate And cell.Value < Date Then

            If cell.Offset(0, -5).Value = "Completed" Then

                    numclosed = numclosed + 1

            End If

        End If

    Next

Sheets("Tasks_to_do").Select

    Dim lrttd As Long
    Dim r As Long
    Dim numttd As Long
    Dim svrange As Range
    Dim resp As String, svname As String

    lrttd = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.count - 1, 0).End(xlUp).Row
    numnttd = lrttd - 1

    Set svrange = Range("K2:K" & lrttd)

    For Each cell In svrange
        If cell.Value = "" Then
            numnew = numnew + 1
            resp = cell.Offset(0, -6).Value
'ERROR ON THIS LINE                
            cell.Value = svname(resp)
        End If
    Next

    msg = MsgBox("*************************" & Chr(13) & "Items closed yesterday: " & numclosed & Chr(13) & "New items as of now: " & numnew, vbOKOnly, "Mproduct item activity")

End Sub

函数svname(resp As String)As String

Dim respdb As Range
lrs As Long

Sheets("Supervisors").Select
    lrs = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.count - 1, 0).End(xlUp).Row
    respdb = Range("A2:A" & lrs)
     For Each cell In respdb
        If cell.Value = resp Then
            svname = cell.Offset(0, 1).Value
            Exit Function
        End If
    Next

结束功能

提前致谢!

1 个答案:

答案 0 :(得分:0)

除了函数之外,您还将svname声明为局部变量。删除第二个声明,它应该工作:

Dim resp As String', svname As String <- error caused by this bit