Excel VBA,无需打开文件即可读取和统计单词(Excel)

我是excel的VBA编码的初学者。 现在,我正在尝试编写一个非常简单的function,即在不打开外部工作簿文件的特定列的情况下对特定单词进行计数。 这个函数将逐行读取。

这是我的代码。 它在特定列中计数“n”。

Public Function ExceptNum(Path As String, Column As String) As Integer Dim count As Integer Dim LastRow As Integer Dim WBook As Workbook Dim i As Long count = 0 Application.ScreenUpdating = False Set WBook = Workbooks.Open(Path) With WBook.Worksheets(1) LastRow = .Range("A" & .Rows.count).End(xlUp).Row For i = 1 To LastRow If .Range(Column & i).Value = "n" Then count = count + 1 End If Next i End With WBook.Close SaveChanges:=False ExceptNum = count Application.ScreenUpdating = True End Function 

但是,当我在工作表中使用我的函数时,它将返回#VALUE !. 我不确定我做错了什么。 代码可以在VBA代码中使用。 但在工作表中,它不起作用。

而且,这个函数会读取一千个文件。 我的function是否高效快捷?