简体   繁体   English

传入null日期的Excel VBA函数会导致#VALUE! 错误

[英]Excel VBA function passing in null date causes #VALUE! error

I have a VBA function (DecTime) that I call passing in the value of a cell. 我有一个VBA函数(DecTime),我调用传递一个单元格的值。 The cell is formatted as custom hh:mm 单元格格式为自定义hh:mm

in my cell the formula is "=DecTime(M6)" 在我的单元格中公式为“= DecTime(M6)”

If M6 is a time, eg 01:05 then it works fine, if it is null then I get #VALUE! 如果M6是时间,例如01:05然后它工作正常,如果它是null然后我得到#VALUE!

I am sure it's a simple solution but having spent the last hour trying lots of things from here and google I am baffled! 我相信这是一个简单的解决方案,但花了最后一小时从这里尝试了很多东西和谷歌我感到困惑!

Here is my function : 这是我的功能:

    Function DecTime(Optional time As Date = #12:00:00 AM#) As Single  'String

    Dim Hours As Integer
    Dim Minutes As Single
    Dim HoursStr As String
    Dim arrTime

'On Error Resume Next
'On Error GoTo error_handler

'   HoursStr = Format(time, "h:mm")

' DecTime = HoursStr

    If time = #12:00:00 AM# Then
'    If HoursStr = "12:00" Then
'    If IsEmpty(time) Then
'    If IsEmpty(time) = True Then
'    If IsNull(time) Then
'    If arrTime.Count = 0 Then
'    If InStr(0, time, ":") = 0 Then
'    If IsDate(time) = False Then
    DecTime = 88
'       DecTime = HoursStr
    Else

    arrTime = Split(time, ":")

    If arrTime(1) <= 0 Then
        Minutes = 0
    ElseIf arrTime(1) <= 5 Then
        Minutes = 0.1
    ElseIf arrTime(1) <= 10 Then
        Minutes = 0.2
    ElseIf arrTime(1) <= 15 Then
        Minutes = 0.3
    ElseIf arrTime(1) <= 20 Then
        Minutes = 0.3
    ElseIf arrTime(1) <= 25 Then
        Minutes = 0.4
    ElseIf arrTime(1) <= 30 Then
        Minutes = 0.5
    ElseIf arrTime(1) <= 35 Then
        Minutes = 0.6
    ElseIf arrTime(1) <= 40 Then
        Minutes = 0.7
    ElseIf arrTime(1) <= 45 Then
        Minutes = 0.8
    ElseIf arrTime(1) <= 50 Then
        Minutes = 0.8
    ElseIf arrTime(1) <= 55 Then
        Minutes = 0.9
    Else
        Minutes = 0
    End If

    Hours = arrTime(0)

    DecTime = Hours + Minutes
'       DecTime = HoursStr

    End If

'error_handler:
'    DecTime = 99
'Resume Next

End Function

在此输入图像描述

As you can see from the remarked code I have tried lots of different options to deal with a blank parameter passed in so if someone can tell me what I've done wrong I'd be very greatful! 正如您从已注释的代码中看到的那样,我尝试了很多不同的选项来处理传入的空白参数,所以如果有人能告诉我我做错了什么我会非常感激!

I am a sql programmer so not much experience with VB 我是一名sql程序员,所以对VB的经验不多

Assuming you want to return 0 if the cell is empty or doesn't contain a date, you could use: 假设您想要返回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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM