2017-07-02 49 views
2

这是我的函数:为什么我的功能可以在Windows上使用,但不能在OS X上使用?

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!错误。

+0

您在每个平台上使用哪种版本的Excel? – brandonscript

+0

@brandonscript excel 2016在这两个 – StB

+0

它适用于Excel 2011中的我。您是否使用它作为UDF或在程序中? –

回答

3

用我的功能看起来像这样的公式IM:= SUM(INDIRECT( “ 'SHEETNAME' C!” & MATCH($ C22; 'SHEETNAME' $ A:$ A,0)&“ :C“& FindLast($ C22))) - StB 11分钟前

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

.Find在Excel 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 
+1

非常感谢! :) – StB

相关问题