我有一个大的嵌套IF AND语句,我想避免在vba中使用它,因为它无法管理。
我将使用一个简单的例子:
Range("O2").Formula = "=IF(M2>TODAY(),""VALIDATED"",IF(AND(N2="""",M2=""""),""NEW ITEM"",IF(AND(N2=""NEW ITEM"",M2<TODAY()),""NOT VALIDATED"")))
我可以使用哪些VBA代码,因为要添加更多的IF语句。另外,我想避免使用循环,因为会有1000行。
更新...我添加了一些新代码,但代码遍历每个单元格,可能会对宏的运行时间产生影响。有什么方法可以加快速度吗?
Set Col = Range("O2:O" & lastrowOU3)
For Each Cell In Col
If Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = 0 Then Cell.Value = "NEW ITEM"
If Cell.Offset(0, -2) = 0 And Cell.Offset(0, -1) = "NEW ITEM" Then Cell.Value = "NOT VALIDATED"
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NEW ITEM" Then Cell.Value = "NOT VALIDATED"
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "VALIDATED" Then Cell.Value = "NOT VALIDATED"
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED" Then Cell.Value = "NOT VALIDATED 1"
If Cell.Offset(0, -2) < Date And Cell.Offset(0, -1) = "NOT VALIDATED 1" Then Cell.Value = "NOT VALIDATED 2"
If Cell.Offset(0, -2) >= Date Then Cell.Value = "VALIDATED"
Next
任何帮助都将不胜感激。
干杯,
康纳
答案 0 :(得分:1)
this = Format(Now, "yyyy-mm-dd")
Set rNg = ThisWorkbook.Sheets("Sheet1").Range("m1:m200")
For Each rCell in rNg.Cells
If rCell.Value > Weekday(This) Then rCell.Offset(0,2).Value = "Validated"
If rCell.Value = "" And rCell.Offset(0,1) ="" Then rCell.Offset(0,2).Value = "New Item"
If rCell.Value < Weekday(This) And rCell.Offset(0,1) ="New Item" Then rCell.Offset(0,2).Value = "Not Validated"
Next rCell
调暗你自己的变量,可能''''不能用“.value”进行重新调整,但无论如何你都没有尝试自己的代码。这就是你要找的东西:P
答案 1 :(得分:1)
我弄清楚了IF语句的代码,但是这会在每个单元格中运行,这可能非常耗时。反正有加速吗?
{% extends "base.html" %}
{% block title %}{{ topic.title }}{% endblock %}
{% block content %}
<h1>Stocks associated with {{ topic.title }}.</h1>
{% for related_stock in related_stocks %}
<ul>
<a href="{{ related_stock.get_absolute_url }}">
{{ related_stock.ticker }}
</a>
</ul>
{% endfor %}
{% endblock %}