1
Public Function GetRowToWriteOn(ByVal SheetName As String, ByVal id As Integer) As Integer
    LastRow = ActiveSheet.UsedRange.Rows.Count
    myarray = Sheets(SheetName).Range("d7:d" & LastRow).Value
    For row = 1 To UBound(myarray, 1)
        If (myarray(row, 1) = id) Then
            Return row
        End If
    Next
End Function

IDE 说预期的语句结束,我该怎么做?(返回 id 相同的行?)

我对 VBA 一点也不熟悉,但是当我从微软看这个例子时,这应该可以吗?:

Return 语句同时分配返回值并退出函数。以下示例显示了这一点。

Function myFunction(ByVal j As Integer) As Double
   Return 3.87 * j
End Function
4

2 回答 2

6

在 VBA 中,返回值不是通过 return 关键字完成的,因为它在其他语言中是自定义的。尝试:

GetRowToWriteOn = row  

函数名本身就充当一个变量。

于 2012-06-28T08:20:31.523 回答
0
Public Function GetRowToWriteOn(ByVal SheetName As String, ByVal idnr As Integer) As Integer
    LastRow = ActiveSheet.UsedRange.Rows.Count
    myarray = Sheets(SheetName).Range("d7:d" & LastRow).Value
    For row = 1 To UBound(myarray, 1)
        If (myarray(row, 1) = idnr) Then
            GetRowToWriteOn = row
            Exit Function
        End If
    Next
    GetRowToWriteOn = UBound(myarray, 1) + 1
    Exit Function 'Probably not needed :)
End Function
于 2012-06-28T08:48:42.430 回答