0

我有五列,其值可能为“True”、“False”、“Bill-to Not in POVA”或“Logic Code Incorrect”,需要解释这五列并根据不同的列返回一个值一个层次结构。

到目前为止,我只为一次单元格开发了 If/Elseif 语句。我知道我可以循环遍历单元格,但是这段代码现在运行了大约 5,000 行,并且随着月份的进展可能会增加到 15,000 行。

有没有人有关于有效方法的建议?请注意,“规则”指的是我在代码的前面部分中命名的命名范围。

Sub Finalize_Formulas()

    Dim Rules As Range
    Set Rules = Range("Rules")

    Dim NumRecords As Long
    NumRecords = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Range("B" & Rows.Count).End(xlUp).Row


'First, copy and paste special (values only) so that we aren't working with formulas anymore
Cells.Copy
Cells.PasteSpecial xlPasteValues

'Now let's interpret the results in Column F based on Columns G-K (Rules 1-5)
Rules.Select
Rules(1, 1).Select
Rules(1, 1).Activate


If Rules(1, 1).OffSet(0, 1) = "True" And _
    Rules(1, 1).OffSet(0, 2) = "True" And _
    Rules(1, 1).OffSet(0, 3) = "True" And _
    Rules(1, 1).OffSet(0, 4) = "True" And _
    Rules(1, 1).OffSet(0, 5) = "True" Then
    Rules(1, 1).Value = "True"
ElseIf Rules(1, 1).OffSet(0, 1) = "Bill-to Not in POVA" Or _
    Rules(1, 1).OffSet(0, 2) = "Bill-to Not in POVA" Or _
    Rules(1, 1).OffSet(0, 3) = "Bill-to Not in POVA" Or _
    Rules(1, 1).OffSet(0, 4) = "Bill-to Not in POVA" Or _
    Rules(1, 1).OffSet(0, 5) = "Bill-to Not in POVA" Then
    Rules(1, 1).Value = "Bill-to Not in POVA"
ElseIf Rules(1, 1).OffSet(0, 1) = "Logic Code Incorrect" Or _
    Rules(1, 1).OffSet(0, 2) = "Logic Code Incorrect" Or _
    Rules(1, 1).OffSet(0, 3) = "Logic Code Incorrect" Or _
    Rules(1, 1).OffSet(0, 4) = "Logic Code Incorrect" Or _
    Rules(1, 1).OffSet(0, 5) = "Logic Code Incorrect" Then
    Rules(1, 1).Value = "Logic Code Incorrect"
ElseIf Rules(1, 1).OffSet(0, 1) = "False" Or _
    Rules(1, 1).OffSet(0, 2) = "False" Or _
    Rules(1, 1).OffSet(0, 3) = "False" Or _
    Rules(1, 1).OffSet(0, 4) = "False" Or _
    Rules(1, 1).OffSet(0, 5) = "False" Then
    Rules(1, 1).Value = "False"
Else

End If

End Sub

我主要关心的是如何让代码对“规则”范围内的每一行执行此检查。以上似乎工作正常,但有没有一种有效的方法来循环遍历范围内的行(大约 5,000 行)或以某种方式一次评估所有内容?

我确实找到了这个解决方案,这并不完全是我所需要的,但强调了一种可能更快的遍历行的方法: https ://stackoverflow.com/a/8178637/2883304

4

2 回答 2

1

你可以尝试这样的事情,而不是你的If statement

If Application.CountIf(Range(Rules(1, 1).Offset(0,1), Rules(1, 1).Offset(0,5)), "True*") = 5 Then
    Rules(1, 1).Value = "True"
ElseIf Application.CountIf(Range(Rules(1, 1).Offset(0,1), Rules(1, 1).Offset(0,5)), "Bill-to Not in POVA") >0 Then
    Rules(1, 1).Value = "Bill-to Not in POVA"
ElseIf Application.CountIf(Range(Rules(1, 1).Offset(0,1), Rules(1, 1).Offset(0,5)), "Logic Code Incorrect") >0 Then
    Rules(1, 1).Value = "Logic Code Incorrect"
ElseIf Application.CountIf(Range(Rules(1, 1).Offset(0,1), Rules(1, 1).Offset(0,5)), "False*") >0 Then
    Rules(1, 1).Value = "False"
Else
End If

但是,请查看TrueFalse- 由于我的测试,在检查 . 时需要添加星号CountIf condition

于 2013-11-06T16:58:19.853 回答
0

以 KazJaw 的回答为基础

你不需要IF/ELSE或循环来实现你想要的。您可以使用该AUTOFILL方法来实现您想要的。

逻辑

如果说值在单元格中G2:K2,那么您实际尝试的是这个公式

=IF(COUNTIF(G2:K2,TRUE)=5,TRUE,IF(COUNTIF(G2:K2,FALSE)=5,FALSE,IF(COUNTIF(G2:K2,"Bill-to Not in POVA")=5,"Bill-to Not in POVA",IF(COUNTIF(G2:K2,"Logic Code Incorrect")=5,"Logic Code Incorrect",""))))

只需在 VBA 中使用它。

这是一个例子。

假设您有来自 的数据G2:K1000,那么只需使用此代码

Sub Sample()

    With ThisWorkbook.Sheets("Sheet1")
        .Range("F2:F1000").Formula = "=IF(COUNTIF(G2:K2,TRUE)=5,TRUE,IF(COUNTIF(G2:K2,FALSE)=5" & _
                                     ",FALSE,IF(COUNTIF(G2:K2,""Bill-to Not in POVA"")=5,""Bill" & _
                                     "-to Not in POVA"",IF(COUNTIF(G2:K2,""Logic Code Incorrect"")" & _
                                     "=5,""Logic Code Incorrect"",""""))))"

        .Range("F2:F1000").Value = .Range("F2:F1000").Value
    End With
End Sub
于 2013-11-06T17:12:58.030 回答