代码之家  ›  专栏  ›  技术社区  ›  Somenath Sinha

编译错误:excel中需要函数或变量

  •  0
  • Somenath Sinha  · 技术社区  · 7 年前

    我有一个函数,它需要一个时间,例如05:00:00,并用当前时间(和可选的另一个时间)检查它,如果参数中的时间已经过了,它应该返回相同的时间,但是是第二天。

    例如,如果 tm = 05:00:00 现在= 2018-08-14 12:00:00 2018-08-15 05:00:00 ,但如果时间还没有过去,例如。 tm = 19:00 2018-08-14 19:00 .

    'Day Adjustment
    Sub DayAdjust(tm As Double, Optional tm2 As Double = 0)
        If tm2 = 0 Then
            tm2 = DayAdjust(tm)
        End If
    
        If (Date + tm) < Now() Or (Date + tm) < tm2 Then
            tm = (Date + 1 + tm)
        End If
        MsgBox tm
        DayAdjust = tm
    End Sub
    

    Dim schStart, schEnd As Double
    schStart = Range("cdh_schStart").Value
    schStartTime = DayAdjust(CDbl(schStart))
    schEnd = Range("cdh_schEnd").Value
    schEndTime = DayAdjust(schEnd, schStart)
    

    牢房 cdh_schStart 包含值 05:00:00 ,和 cdh_schEnd 08:00:00 当前系统时间设置为 2018-08-14 18:30:00

    在第三行, schStartTime = DayAdjust(CDbl(schStart))

    编译错误:

    期望的函数或变量

    1 回复  |  直到 7 年前
        1
  •  2
  •   QHarr    7 年前

    您正在尝试与 schStartTime = DayAdjust(CDbl(schStart)) 所以你的潜艇需要作为一个函数来编写。实际上,您的sub中有一个返回值赋值,但我猜您输入错了或没有意识到它必须声明为一个函数。

    @Rory re potential提出了一个很好的点,如果传递为零或未提供tm2,则无限循环的值为零,因为函数将在没有中断的情况下递归调用。你需要找到一个合适的方法来处理这个问题。

    Public Function DayAdjust(tm As Double, Optional tm2 As Double = 0) As Variant
        If tm2 = 0 Then
            tm2 = DayAdjust(tm)
        End If
    
        If (Date + tm) < Now() Or (Date + tm) < tm2 Then
            tm = (Date + 1 + tm)
        End If
        MsgBox tm
        DayAdjust = tm
    End Function