各位高手,请帮忙解决如下问题,多谢!VBA:Run-time error 13:Type mismatch when using CountIf()

xukaili 2011-04-15 09:22:13
各位高手,请帮忙解决如下问题,多谢!

程序如下: 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正确运行,多谢!!!
...全文
379 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

6,210

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office应用
社区管理员
  • Microsoft Office应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧