使用select case语句评估多个列

时间:2019-05-13 17:19:01

标签: excel vba

我正在尝试从各个列中评估多条信息,然后在答案满足特定条件时输出值。

我曾尝试做一个扩展的select case语句,但是我知道我的语法是错误的,或者不可能以我尝试的方式进行。它还引发错误“类型不匹配”。不是完整的代码,而是一部分。

Dim EJ As String
Dim EL As String
Dim EX As String
Dim EZ As String
Dim EG As String

EJ = GetCellValue(.Cells(i, "EJ"))
EL = GetCellValue(.Cells(i, "EL"))
EX = GetCellValue(.Cells(i, "EX"))
EZ = GetCellValue(.Cells(i, "EF"))
EG = GetCellValue(.Cells(i, "EG"))

Select Case EJ And EL And EX And EZ
   Case GetCellValue(.Cells(i, "EJ")) = "YES" And _
        GetCellValue(.Cells(i, EL")) "YES" And _
        GetCellValue(.Cells(i, "EX")) = "YES" And _
        GetCellValue(.Cells(i, "EZ")) = "YES"
           .Cells(i, "FI") = Done

如果满足条件,我想在一栏中输出完成的单词。

3 个答案:

答案 0 :(得分:1)

Select Case可能不是最佳选择-常规的If...ElseIf...Else会很好

 If EJ="YES" And EL="YES" And EX="YES" And EZ="YES" Then
     .Cells(i, "FI") = "Done" '<< added quotes here
 Else
     'etc etc
 End If

这可能与您的其他测试的外观有很大不同

答案 1 :(得分:0)

您还可以使用过滤器完成任务...

With Range("A1").CurrentRegion 'change to meet your range  
    .AutoFilter
    .AutoFilter Field:=140, Criteria1:="YES"
    .AutoFilter Field:=142, Criteria1:="YES"
    .AutoFilter Field:=154, Criteria1:="YES"
    .AutoFilter Field:=156, Criteria1:="YES"
    On Error Resume Next

    Dim rng As Range
    Set rng = Range("Z2:Z" & Cells(Rows.Count, "Z").End(xlUp).Row)
    Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)
    rng.Offset(, 9).Value = "Done"
End With

注意:将文本插入可见行的列中而不循环,是根据加里学生(Gary's Student)的答案改编为28609977。

答案 2 :(得分:0)

可以使用Excel公式避免循环。例如,如果i是2到9:

.Range("FI2:FI9").Formula = "=IF(AND(EJ2=""YES"", EL2=""YES"", EX2=""YES"", EZ2=""YES""), ""Done"", """")"
.Range("FI2:FI9").Value = .Range("FI2:FI9").Value  ' optional to convert the formulas to values