我有五列,其值可能为“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