gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:29:33 27 4
gpt4 key购买 nike

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

例子: 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com