VBA CountIf 外部文档

VBA CountIf external document

根据我之前的 ,我现在正在努力创建一个有效的 Application.CountIf 函数。我正在使用以下代码以 "xl0":

的形式访问文件
'DATABASE ACCESS
Dim xl0 As New Excel.Application
Dim xlw As New Excel.Workbook
Dim db_directory As String
db_directory = "R:\New Quality Management System\xls\Supplier\Non-Conformance\Supplier Non-Conformance Database.xlsm"
Set xlw = xl0.Workbooks.Open(db_directory)

我可以创建一个函数来搜索同一个打开的文档没问题...

Test = Application.CountIf(Range("B:B"), Report_ID)
MsgBox (Test)

...但是我尝试在后台打开的文档中搜索的方法都没有用...

Test = Application.CountIf(xlw.Sheets("SNCR Log").Range("B:B"), Report_ID)

...和...

Test = xlw.Sheets("SNCR Log").Application.CountIf(Range("B:B"), Report_ID)

我错过了什么?

只是猜测,没有测试,但是因为 Application 指的是你当前打开的应用程序,你想在 xl0 应用程序中搜索尝试

xl0.CountIf(...)

看看是否有帮助。