为什么我的function在Windows上运行,而不是在OS X上运行?

这是我的function:

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! 单元格中的错误。

公式im使用我的函数如下所示:= SUM(INDIRECT(“'sheetname'!C”&MATCH($ C22;'sheetname'!$ A:$ A; 0)&“:C”&FindLast($ C22 ))) – StB 11分钟前

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

在Excel中findMac UDF不起作用。 它不会像我在上面的评论中提到的那样给你提供错误信息。 它会给你0 。 这是另一个确认相同的stackoverflowpost 。

为了使它工作,你将不得不使用如下所示的循环。

 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