如果单元格值在数组内,则返回true,否则返回false

时间:2019-07-01 05:26:49

标签: excel vba

如果我传递了dayVal和其他序列字符串以使其起作用,则它应该返回TrueFalse

我都无法弄清楚。

这是我的代码

Function IsWeekOff(ByVal weekDayVal As Variant, ByVal WorkingSequence As Variant) As Boolean
    Dim Mon_Fri, Sun_Thu, Thu_Mon, Tue_Sat As Variant
    Mon_Fri = Array("Mon", "Tue", "Wed", "Thu", "Fri")
    Sun_Thu = Array("Sun", "Mon", "Tue", "Wed", "Thu")
    Thu_Mon = Array("Thu", "Fri", "Sat", "Sun", "Mon")
    Tue_Sat = Array("Tue", "Wed", "Thu", "Fri", "Sat")

    Dim El1, El2, El3, El4 As Variant
    If WorkingSequence = Mon_Fri Then
        For Each El1 In Mon_Fri
          If El1 = weekDayVal Then
                IsWeekOff = True
                Exit Function
          End If
        Next El1      
    ElseIf WorkingSequence = Sun_Thu Then
        For Each El2 In Sun_Thu
          If El2 = weekDayVal Then
                IsWeekOff = True
                Exit Function
          End If
        Next El2
    ElseIf WorkingSequence = Thu_Mon Then
        For Each El3 In Thu_Mon
          If El3 = weekDayVal Then
                IsWeekOff = True
                 Exit Function
          End If
        Next El3
    ElseIf WorkingSequence = Tue_Sat Then
       For Each El4 In Tue_Sat
          If El4 = weekDayVal Then
                IsWeekOff = True
                 Exit Function
          End If
        Next El4
    End If
    IsWeekOff = False
End Function

Excel文件

See Image for excel table

1 个答案:

答案 0 :(得分:0)

问题:您在If语句中缺少引号:

当您从excel工作表传递字符串时,它应该带有引号。 If WorkingSequence = "Mon_Fri"

Function IsWeekOff(ByVal weekDayVal As Variant, ByVal WorkingSequence As Variant) As Boolean

    Dim Mon_Fri, Sun_Thu, Thu_Mon, Tue_Sat As Variant
    Mon_Fri = Array("Mon", "Tue", "Wed", "Thu", "Fri")
    Sun_Thu = Array("Sun", "Mon", "Tue", "Wed", "Thu")
    Thu_Mon = Array("Thu", "Fri", "Sat", "Sun", "Mon")
    Tue_Sat = Array("Tue", "Wed", "Thu", "Fri", "Sat")

    Dim El1, El2, El3, El4 As Variant
    If WorkingSequence = "Mon_Fri" Then
        For Each El1 In Mon_Fri
          If El1 = weekDayVal Then
                IsWeekOff = True
                Exit Function
          End If
        Next El1
    ElseIf WorkingSequence = "Sun_Thu" Then
        For Each El2 In Sun_Thu
          If El2 = weekDayVal Then
                IsWeekOff = True
                Exit Function
          End If
        Next El2
    ElseIf WorkingSequence = "Thu_Mon" Then
        For Each El3 In Thu_Mon
          If El3 = weekDayVal Then
                IsWeekOff = True
                 Exit Function
          End If
        Next El3
    ElseIf WorkingSequence = "Tue_Sat" Then
       For Each El4 In Tue_Sat
          If El4 = weekDayVal Then
                IsWeekOff = True
                 Exit Function
          End If
        Next El4
    End If
    IsWeekOff = False


End Function

结果:

enter image description here