我有一個VBA函數(DecTime),我調用傳遞一個單元格的值。所述細胞被格式化爲定製爲hh:mm傳入空日期的Excel VBA函數會導致#VALUE!錯誤
在我的小區中的公式爲「= DecTime(M6)」
如果M6是一個時間,例如01:05然後正常工作,如果是空值,則我獲得了#VALUE!
我相信這是一個簡單的解決方案,但花了最後一個小時嘗試從這裏很多東西,谷歌我很困惑!
這裏是我的功能:
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
你可以從該被關注的代碼,我已經試過很多不同的選項來處理傳入的一個空白的參數看,所以如果有人能告訴我我做錯了我會非常棒!
我是一個SQL程序員,所以用VB
是什麼在M6當你認爲它是空的?一個空單元格不會導致該代碼的錯誤。 – Rory
這只是空白,添加了屏幕截圖...雖然它是一個調用 –
這是計算導致錯誤,如果我刪除它的單元格,它工作正常! –