如何处理Excel中跨列的多个IFTHEN查询?

如何处理Excel中跨列的多个IFTHEN查询?,excel,vba,Excel,Vba,我正在尝试创建一个分配系统,而在我被卡住之前,我已经编程了VBA 我有五列数据,其中的内容决定了我分配给他们的工作人员 我需要做的是: If column E = "a", then put name "xx" in column A (of same row) If column E = "b", then put name "yy" in column A (of same row) If column E is blank, then move to next criteria....

我正在尝试创建一个分配系统,而在我被卡住之前,我已经编程了
VBA

我有五列数据,其中的内容决定了我分配给他们的工作人员

我需要做的是:

If column E = "a", then put name "xx" in column A (of same row)
If column E = "b", then put name "yy" in column A (of same row)
If column E is blank, then move to next criteria....

If column D is "c" then put name "zz" in column A (of same row)
If column D is "d" then move to next criteria....

If column A is blank then put name "ww" in column A.
提前谢谢

Sub Allocate()

    Dim i As Long

    For i = 1 To Range("E" & Rows.Count).End(xlUp).Row
        If Range("E" & i) = "a" Then
            Range("A" & i) = "xx"
        ElseIf Range("E" & i) = "b" Then
            Range("A" & i) = "yy"
        End If

        If Range("D" & i) = "c" Then
            Range("A" & i) = "zz"
        End If

        If IsEmpty(Range("A" & i)) Then
            Range("A" & i) = "ww"
        End If
    Next i

End Sub
注意

如果E列为空,则移动到下一个标准….


没有包含在代码中的任何地方,在代码中实现这一点是没有意义的,因为它实际上什么都不做。:)

就像我在您的问题下的链接中提到的,您不需要为此进行循环

如果您使用Excel公式,那么您将使用以下内容

=IF(E1="a","xx",IF(E1="b","yy",IF(D1="c","zz","ww")))
只需在vba代码中使用它

Option Explicit

Sub Sample()
    Dim lRow As Long

    '~~> Change this to the relevant sheet
    With ThisWorkbook.Sheets("Sheet1")
        lRow = .Range("E" & .Rows.Count).End(xlUp).Row

        .Range("A1:A" & lRow).Formula = "=IF(E1=""a"",""xx"",IF(E1=""b"",""yy"",IF(D1=""c"",""zz"",""ww"")))"

        .Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value
    End With
End Sub
屏幕截图


或者您可以使用
工作表\u Change
event感谢Siddarth,但我在那个长公式中有点迷路了,我想我必须用我的值来尝试并出错。