Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2409|回复: 3

[已解决]为什么这里的COUNTIF无效?

[复制链接]
发表于 2016-4-23 18:01 | 显示全部楼层 |阅读模式
本帖最后由 lingyuncelia1 于 2016-4-23 21:36 编辑

捕获.PNG
Sub celia()
For i = 1 To 2
If TimeValue("6:0:0") < Cells(i, 1) < TimeValue("7:0:0") Then
MsgBox "ok"
End If
mycount = Application.WorksheetFunction.CountIf(Columns("a:a"), TimeValue("6:0:0") < Cells(i, 1) < TimeValue("7:0:0"))
If mycount >= 1 Then MsgBox "pass"
Next
End Sub
为什么红体字代码运行无效?


最佳答案
2016-4-23 18:09
Sub celia()
For i = 1 To 2
If TimeValue("6:0:0") < Cells(i, 1) and cells(i,1)< TimeValue("7:0:0") Then
MsgBox "ok"
End If
mycount = Application.WorksheetFunction.CountIfs(Columns("a:a"), ">"&TimeValue("6:0:0"),Columns("a:a"), "<" & TimeValue("7:0:0"))
If mycount >= 1 Then MsgBox "pass"
Next
End Sub

雨碎江南.zip

10.76 KB, 下载次数: 4

发表于 2016-4-23 18:05 | 显示全部楼层
表示一个数字在另二个数字区间内的表达式根本不是这么写的,不管公式还是在VBA中

评分

参与人数 1 +1 收起 理由
lingyuncelia1 + 1

查看全部评分

回复

使用道具 举报

发表于 2016-4-23 18:05 | 显示全部楼层
首先。。。要选择在两个时间之间不能用数学表达式 a<x<b
这样表示的,必须要分开,把他变成程序能解读的代码: a<x and x<b
在你的countif 里 则是要改成countifs函数。。。

评分

参与人数 1 +3 收起 理由
lingyuncelia1 + 3

查看全部评分

回复

使用道具 举报

发表于 2016-4-23 18:09 | 显示全部楼层    本楼为最佳答案   
Sub celia()
For i = 1 To 2
If TimeValue("6:0:0") < Cells(i, 1) and cells(i,1)< TimeValue("7:0:0") Then
MsgBox "ok"
End If
mycount = Application.WorksheetFunction.CountIfs(Columns("a:a"), ">"&TimeValue("6:0:0"),Columns("a:a"), "<" & TimeValue("7:0:0"))
If mycount >= 1 Then MsgBox "pass"
Next
End Sub

评分

参与人数 1 +3 收起 理由
lingyuncelia1 + 3

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-28 09:19 , Processed in 0.287847 second(s), 16 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表