检查范围内两个条件的算法
Checking algorithm with two conditions in ranges
我在使用范围内的两个条件检查算法时遇到问题...
我想在检查范围内的两个条件后分配一个值...
示例:
我想为 D 列赋值...并且我有 ID 为 (1,2,3...) 的编号范围。 B 列仅在 ID = 1 附近有值,下一个 ID 为空,但范围内的所有行都有字符。
所以,我需要根据旁边的table给D列赋值。
当 B 列为 TRUE 时,D 列中的值 = NEGATIVE ... 结束,但当 B 列中的值 = FALSE 检查范围内的组合时。
预期结果:
你有什么想法吗?感谢您的帮助!
"D1"中的公式如下:
=IF(B1=TRUE,"NEGATIVE",IF(B1="","", C1))
粘贴表格 "D1"
=IF(ISBLANK(B1),"", IF(B1="TRUE", "NEGATIVE", C1))
炸药爆炸了。这将采用这样的输入:
然后给你这个:
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
我在使用范围内的两个条件检查算法时遇到问题... 我想在检查范围内的两个条件后分配一个值...
示例:
我想为 D 列赋值...并且我有 ID 为 (1,2,3...) 的编号范围。 B 列仅在 ID = 1 附近有值,下一个 ID 为空,但范围内的所有行都有字符。 所以,我需要根据旁边的table给D列赋值。 当 B 列为 TRUE 时,D 列中的值 = NEGATIVE ... 结束,但当 B 列中的值 = FALSE 检查范围内的组合时。
预期结果:
你有什么想法吗?感谢您的帮助!
"D1"中的公式如下:
=IF(B1=TRUE,"NEGATIVE",IF(B1="","", C1))
粘贴表格 "D1"
=IF(ISBLANK(B1),"", IF(B1="TRUE", "NEGATIVE", C1))
炸药爆炸了。这将采用这样的输入:
然后给你这个:
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