为什么我的功能在Windows上运行而在OS X上运行?

时间:2017-07-02 14:07:32

标签: excel excel-vba excel-vba-mac vba

这是我的功能:

Function FindLast(searchTerm As String) As Integer

    Set cell = Worksheets("nameofsheet").Columns("A").Find(what:=searchTerm, searchorder:=xlByColumns, searchdirection:=xlPrevious)

    If cell Is Nothing Then
      Debug.Print ("Text was not found")
        FindLast = 0
    Else
      Debug.Print ("found")
        FindLast = cell.row
    End If

End Function

它在Windows上运行得很好,但在Mac上却没有。我总是在单元格中出现#REF!错误。

1 个答案:

答案 0 :(得分:3)

  

使用我的函数的公式如下所示:= SUM(间接("' sheetname'!C"& MATCH($ C22;' sheetname&#39) ; $ A:$ A; 0)&":C"& FindLast($ C22))) - StB 11分钟前

错误不是因为您的代码。这是因为INDIRECT()

Excel中的

.Find Mac UDF无法正常工作。它不会像我在上面的评论中提到的那样给你错误信息。它会给你0。这是另一个stackoverflow post,它确认了相同的内容。

要使其正常工作,您必须使用如下所示的循环。

Function FindLast(searchTerm As String) As Integer
    Dim oSht As Worksheet
    Dim lastRow As Long, i As Long

    On Error GoTo WHOA

    Set oSht = Sheets("nameofsheet")

    lastRow = oSht.Range("A" & oSht.Rows.Count).End(xlUp).Row

    For i = 1 To lastRow
        If oSht.Range("A" & i).Value = searchTerm Then
            MsgBox "Value Found in Cell " & oSht.Range("A" & i).Address
            FindLast = i
            Exit Function
        End If
    Next i
    Exit Function
WHOA:
    MsgBox Err.Description
End Function