使用If,ElseIf循环遍历单元格的更快方法

时间:2013-11-06 16:43:42

标签: excel-vba vba excel

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

到目前为止,我只为一次单元格开发了If / Elseif语句。我知道我可以遍历单元格,但是这个代码现在大约运行5000行,并且随着月份的进展可以达到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

2 个答案:

答案 0 :(得分: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时需要添加星号。

答案 1 :(得分:0)

以KazJaw的回答为基础

您不需要IF/ELSE或循环来实现您想要的效果。您可以使用AUTOFILL方法来实现您的目标。

<强> LOGIC

如果说值在单元格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