切换synatx错误问题

时间:2016-08-30 08:44:06

标签: vba excel-2010

在excel 2010中的以下vba代码中,我收到错误消息语法错误。我认为这与switch语句有关,但看不出有什么问题。你知道我做错了吗?

Sub timeperiod()

Dim change As Integer, colA As Date, i As Long, val As String

For i = 2 To Rows.Count

colA = Cells(i, 1).Value
change = Day(Now) - Day(colA)
val = Cells(i, 3).Value

If change <= 0 Then

val = Switch(_
DATEDIF (colA, Now,"m") < 3 And DATEDIF (colA, Now,"m") > 0,'1 - 2 months',_
DATEDIF (colA, Now,"m") < 5 And DATEDIF (colA, Now,"m") > 2,'2 - 4 months',_
DATEDIF (colA, Now,"m") < 7 And DATEDIF (colA,Now,"m") > 4,'4 - 6 months',_
DATEDIF (colA, Now,"m") <10 And DATEDIF (colA, Now,"m") > 6, '6 - 9 months',_
DATEDIF (colA, Now,"m") > 9, '9 months+')

ElseIf change > 0 Then

val = Switch (_
DATEDIF (colA, Now,"m") = 2, '2 - 4 months',_
DATEDIF (colA, Now,"m") = 4, '4 - 6 months',_
DATEDIF (colA, Now,"m") = 6, '6 - 9 months',_
DATEDIF (colA, Now,"m") = 9, '9 months+',_
DATEDIF (colA, Now,"m") < 2 And DATEDIF (colA, Now,"m") > 0,'1 - 2 months',_
DATEDIF (colA, Now,"m") < 4 And DATEDIF (colA, Now,"m") > 2,  '2 - 4 months',_
DATEDIF (colA, Now,"m") < 6 And DATEDIF (colA, Now,"m") > 4,  '4 - 6 months',_
DATEDIF (colA, Now,"m") < 9 And DATEDIF (colA, Now,"m") > 6, '6 - 9 months',_
DATEDIF (colA, Now,"m") > 9, '9 months+')

End If
Next i


End Sub

3 个答案:

答案 0 :(得分:3)

几个问题:

使用_继续行:

val = switch( _
    ...

_之前的空格很重要(要启用包含_的变量名称,我猜?)。除了最后一段

之后,您也无法发表评论

使用双引号"创建字符串,单引号'用于评论。

在VBA中使用DateDiff。用法DateDiff(Interval, Date1, Date2)

DateDiff ("m", colA, Now)

然而,正如Tensibai指出的那样,Select ... Case会更好:

Select Case DateDiff("m", colA, Now)
    Case 1 to 2
        val = "1 - 2 months"
    Case 3 to 4
        val = "2 - 4 months"
    '...
    Case Is > 9
        val = "9+ months"
End Select

答案 1 :(得分:2)

Tensibai是正确的SELECT...CASE在这种情况下可能会更好,但是,在VBA中有一个SWITCH - 函数。

您的语法中存在一些小错误:

  1. DateDiff而非DATEDIF
  2. 在VBA中,您必须使用双引号"而不是单引号'来启动和转义字符串。单引号用于评论
  3. DateDiff - 语法为DateDiff(Interval, Date1, Date2),因此您将参数混合了一点
  4. 至于你的代码:

    Sub timeperiod()
    
    Dim change As Integer, colA As Date, i As Long, val As String
    
    For i = 2 To Rows.Count
    
        colA = Cells(i, 1).Value
        change = Day(Now) - Day(colA)
        val = Cells(i, 3).Value
    
        If change <= 0 Then
    
            Debug.Print DateDiff("M", colA, Now)
            Select Case DateDiff("M", colA, Now)
                Case 1 To 2: val = "1 - 2 months"
                Case 2 To 4: val = "2 - 4 months"
                Case Is > 9: val = "9 months"
                ' and so on
                Case Else: val = "not categorized"
            End Select
        ElseIf change > 0 Then
    
        ' your code....
    
        End If
    
        Debug.Print val
    
    Next i
    

    End Sub

答案 2 :(得分:1)

你的语法错误已由Martin和acradeprecinct正确描述,所以我在这里专注于逻辑

Switch()函数从左到右评估表达式,并在第一个评估为True的表达式停止。

您可以利用此功能并缩短代码,如下所示:

Option Explicit

Sub timeperiod()
    Dim change As Integer, colA As Date, i As Long, val As String
    Dim dateDif As Long

    For i = 2 To Rows.Count
        colA = Cells(i, 1).value
        dateDif = DateDiff("m", colA, Now)
        change = Day(Now) - Day(colA)
        If change <= 0 Then
            val = Switch(dateDif <= 0, "Error", _
                         dateDif < 3, "1 - 2 months", _
                         dateDif < 5, "2 - 4 months", _
                         dateDif < 7, "4 - 6 months", _
                         dateDif < 10, "6 - 9 months", _
                         dateDif >= 10, "9 months+")

        ElseIf change > 0 Then
            val = Switch(dateDif < 0, "Error", _
                         dateDif < 2, "1 - 2 months", _
                         dateDif < 4, "2 - 4 months", _
                         dateDif < 6, "4 - 6 months", _
                         dateDif < 9, "6 - 9 months", _
                         dateDif >= 9, "9 months+")
        End If
    Next i
End Sub

我指出你的&#34;标签&#34;实际上彼此重叠,我认为你应该考虑更改第一个数字,以便第一个数字Switch

        val = Switch(dateDif <= 0, "", _
                     dateDif < 3, "1 - 2 months", _
                     dateDif < 5, "3 - 4 months", _
                     dateDif < 7, "5 - 6 months", _
                     dateDif < 10, "7 - 9 months", _
                     dateDif >= 10, "10 months+")

并且类似于其他Switch语句

也就是说,一旦您发现两个Switch()语句仅对dateDif的比较术语有所不同,您可以进一步缩短代码,与change值相比0 1}}

所以你可以编码:

Option Explicit

Sub timeperiod()
    Dim change As Integer, colA As Date, i As Long, val As String
    Dim dateDif As Long, deltaDif As Long

    For i = 2 To Rows.Count
        colA = Cells(i, 1).value
        dateDif = DateDiff("m", colA, Now)
        change = Day(Now) - Day(colA)
        deltaDif = IIf(change <= 0, 1, 0)

        val = Switch(dateDif < 0, "Error", _
                     dateDif < 2 + deltaDif, "1 - 2 months", _
                     dateDif < 4 + deltaDif, "2 - 4 months", _
                     dateDif < 6 + deltaDif, "4 - 6 months", _
                     dateDif < 9 + deltaDif, "6 - 9 months", _
                     dateDif >= 9 + deltaDif, "9 months+")
    Next i
End Sub