如何在Excel中跨列处理多个IFTHEN查询?
我正在尝试创build一个分配系统,而我在这个时候卡住了之前编写了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
注意
If column E is blank, then move to next criteria....
没有包含在代码中的任何地方,没有意义在代码中实现这一点,因为它从字面上什么都不做 🙂
就像我在你的问题下的链接中提到的那样,你不需要循环。
如果你要使用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
ScreenShot :
- Excel csv文件中的string(123)
- 避免和处理“使用中的文件”Excel通知
- 在另一个单元格中返回一个基于月份的单元格值
- Excel VBA:在嵌套的if-else语句中创build“fall through”,还允许正常的行为?
- 如何从单个单元格中获取特定值并将其放入Excel VBA中的不同单元格中
- Python / Pandas:循环访问数字列表
- 将多个不同行的Excel文件合并到pandas的一个Excel文件中
- C ++ Excel :: RangePtr range_dataFloat – expression式必须是一个指向完整对象types的指针
- excel数据库function与vba结合使用,如果没有logging怎么办?