algorithm - 检查范围内两个条件的算法

标签 algorithm vba excel

我在使用范围内的两个条件检查算法时遇到问题... 我想在检查范围内的两个条件后分配一个值...

例子: enter image description here

我想为 D 列分配一个值...并且我有 ID 为 (1,2,3...) 的编号范围。 B 列仅在 ID = 1 附近有值,下一个 ID 为空,但范围内的所有行都有字符。 因此,我需要根据下表为 D 列赋值。 当 B 列为 TRUE 时,D 列中的值 = NEGATIVE... 结束,但当 B 列中的值 = FALSE 时,检查范围内的组合。

预期结果: enter image description here

你有想法吗?感谢您的帮助!

最佳答案

炸药爆炸了。这将需要这样的输入:

enter image description here

然后给你这个:

enter image description here

Sub TestIt()

Dim LastRow As Long, CurRow As Long, InLast As Long, FindRng As Range

LastRow = Range("C" & Rows.Count).End(xlUp).Row

For CurRow = 1 To LastRow
    If Not Range("B" & CurRow).Value = "" Then
        If Range("B" & CurRow + 1).Value = "" Then
            InLast = Range("B" & CurRow).End(xlDown).Row - CurRow - 1
        Else
            InLast = 0
        End If
        If InLast > LastRow Then InLast = LastRow - CurRow
        If InLast > 0 Then Set FindRng = Range(Cells(CurRow, 3), Cells(InLast + CurRow, 3))
        Select Case True
            Case Range("B" & CurRow).Value = "TRUE"
                Range("D" & CurRow).Value = "NEGATIVE"
            Case InLast = 0
                Range("D" & CurRow).Value = Range("C" & CurRow).Value
            Case Not FindRng.Find("POSITIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And Not FindRng.Find("NEGATIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And Not FindRng.Find("NEUTRAL", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And InLast > 0
                Range("D" & CurRow).Value = "MIX"
            Case Not FindRng.Find("POSITIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And FindRng.Find("NEGATIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And InLast > 0
                Range("D" & CurRow).Value = "POSITIVE"
            Case FindRng.Find("POSITIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And Not FindRng.Find("NEGATIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And InLast > 0
                Range("D" & CurRow).Value = "NEGATIVE"
            Case FindRng.Find("POSITIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And FindRng.Find("NEGATIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And Not FindRng.Find("NEUTRAL", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
                    And InLast > 0
                Range("D" & CurRow).Value = "NEUTRAL"
            'Case Not FindRng.Find("POSITIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
            '        And Not FindRng.Find("NEGATIVE", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
            '        And FindRng.Find("NEUTRAL", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
            '        And InLast > 0
            '    Range("D" & CurRow).Value = "CASE POSITIVE AND NEGATIVE NO NEUTRAL"
            Case Else
                Range("D" & CurRow).Value = "ERROR"
        End Select
    End If
Next CurRow

End Sub

关于algorithm - 检查范围内两个条件的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28051057/

相关文章:

excel - Sumif 返回相同的值

vba - 拆分和 VBA.Split

algorithm - 碰撞检测问题(与平面相交)

c - 翻译结构项的有效方法,避免 C 中的 switch-case

python - 如何用python制作随机文本

excel - Excel中基于复杂团队规则的前十名排序

string - 为什么我们不能一次性可靠地测试回文

excel - 对象 'Copy' 的方法 '_Worksheet' 失败

vba - 将 *.xlsm 文件另存为 *.xlsx 并抑制弹出窗口

python - 将数据框保存到 Excel 后,我无法打开 Excel 文件