各位高手,请帮忙解决如下问题,多谢!VBA:Run-time error 13:Type mismatch when using CountIf()
各位高手,请帮忙解决如下问题,多谢!
程序如下: Dim myWorkbook As Excel.Application
Set myWorkbook = New Excel.Application
myWorkbook.Workbooks.Open ("workbookName")
Dim mySheet As Excel.Worksheet
Set mySheet = myWorkbook.sheets("sheet1")
Dim rangeTest As Excel.range
Dim minTest As Variant
Dim maxTest As Variant
Dim countTest As Double
Set rangeTest = mySheet.range(mySheet.Cells(1, 1), mySheet.Cells(10, 1))
countTest = Application.WorksheetFunction.CountIf(rangeTest, ">0")
maxTest = CVar(Application.WorksheetFunction.Max(mySheet.range(mySheet.Cells(1, 1), mySheet.Cells(10, 1))))
问题(1) 在运行至:Application.WorksheetFunction.CountIf(rangeTest, ">0"),出现如下错误:
Run-time error '13':
Type mismatch
而Application.WorksheetFunction.Max()可以正确运行。
问题(2)
Set mySheet = ActiveWorkbook.Worksheets(1)
Set rangeTest = mySheet.range(mySheet.Cells(1, 1), mySheet.Cells(10, 1))
countTest = Application.WorksheetFunction.CountIf(rangeTest, ">0")
如果将mySheet和rangeTest重新赋值,CountIf也可以正确运行。
求教:如何能使问题(1)中的CountIf正确运行,多谢!!!