删除单元格内容,以防发生类型不匹配错误。

huangapple go评论50阅读模式
英文:

Delete the contents of the cell in case of type-mismatch error

问题

我想检查日期并收集超过1年的日期数量。但问题是在检查的列中不仅有正确的日期,例如:2022.04-05(四月-五月)或文本,所以在CDate的情况下我会得到类型不匹配的错误。是否有可能在出现类型不匹配错误的情况下删除单元格内容而不中断宏?我的意思是,如果CDate不可行,删除单元格的内容。谢谢!

英文:

I would like to check dates and collect how many are older than 1 year. But the problem is that in the examined column are not only correct dates, for example: 2022.04-05 (april-may) or text, so in case of CDate I get type-mismatch error. Is there any possibility to delete cells in case of type-mismatch error without interrupting the macro? I mean if CDate is not possible, delete the contents of the cell. Thank you!

答案1

得分: 0

使用 IsDate 函数:
> 如果表达式是日期或可识别为有效日期或时间,则返回 True;否则返回 False

If IsDate(cell.Value) Then
   ' 收集超过1年的数量
Else
   cell.ClearContents
End If
英文:

Using IsDate:
> Returns True if the expression is a date or is recognizable as a valid date or time; otherwise, it returns False.

If IsDate(cell.Value) Then
   ' collect how many are older than 1 year
Else
   cell.ClearContents
End If

huangapple
  • 本文由 发表于 2023年5月25日 21:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定