我有一个VBA函数(DecTime),我调用传递一个单元格的值。单元格格式为自定义hh:mm
在我的单元格中,公式为“= DecTime(M6)”
如果M6是时间,例如01:05,那么它可以正常工作,如果它是null,那么我得到#VALUE!
我确信这是一个简单的解决方案,但是花了最后一小时从这里尝试了很多东西而google我很困惑!
这是我的功能:
name.raw
正如您从已注明的代码中看到的那样,我尝试了很多不同的选项来处理传入的空白参数,所以如果有人能告诉我我做错了什么我会非常感激!
我是一名sql程序员,所以没有多少VB经验
答案 0 :(得分:3)
假设您想要在单元格为空或不包含日期时返回0,您可以使用:
Function DecTime(Optional time = #12:00:00 AM#) As Double
Dim Hours As Integer
Dim Minutes As Single
Dim arrTime
If Not IsDate(time) Then
DecTime = 0
ElseIf time = #12:00:00 AM# Then
DecTime = 0
Else
arrTime = Split(time, ":")
Select Case arrTime(1)
Case Is = 0
Minutes = 0
Case Is <= 5
Minutes = 0.1
Case Is <= 10
Minutes = 0.2
Case Is <= 20
Minutes = 0.3
Case Is <= 25
Minutes = 0.4
Case Is <= 30
Minutes = 0.5
Case Is <= 35
Minutes = 0.6
Case Is <= 40
Minutes = 0.7
Case Is <= 50
Minutes = 0.8
Case Is <= 55
Minutes = 0.9
Case Else
Minutes = 0
End Select
Hours = arrTime(0)
DecTime = Hours + Minutes
End If
End Function