Excel VBA 如果 range.value = 某物,则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40100321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA If range.value = something then
提问by Praveen Behera
What I am looking for is this -
我要找的是这个——
If a value in range of cells equals to some value or same value then it should show "Positive" else "Negative. But when I write like below syntax, it throws an error -
如果单元格范围内的值等于某个值或相同的值,那么它应该显示“正”,否则“负”。但是当我像下面这样写时,它会抛出一个错误 -
If range("F3:H5").value = "X" then
Msgbox "Positive result"
else
Msgbox "Negative result"
end if
回答by SIM
It should serve the purpose:
它应该用于以下目的:
Sub string_validation()
Dim cel As Range
For Each cel In Range("F3:H8")
If cel.Value = "hassle" Then
MsgBox "Positive result"
Else
MsgBox "Negative result"
End If
Next cel
End Sub
回答by Paul
You'll need something like:
你需要这样的东西:
Dim found As Boolean
found = False
For Each cell In Range("F3:h5").Cells
if cell.Value = "X" Then
found = True
end if
Next
If found = True Then
Msgbox "Positive result"
else
Msgbox "Negative result"
End if
回答by ArindamD
You can use this function
你可以使用这个功能
Function EvalRange(inRng As Range, inVal As Variant) As Variant
Dim CntAll, CntMatch As Double
CntAll = Application.Count(inRng)
CntMatch = Application.CountIf(inRng, inVal)
If CntAll = CntMatch Then
EvalRange = "Positive Result"
Else: EvalRange = "Negative Result"
End If
End Function
回答by 1Hyman
VBA.MsgBox VBA.IIf(Evaluate("=SUMPRODUCT(--(F3:H5 = ""X""))"), "Positive result", "Negative result")
""X"" double quotes due to two outer quotes
""X"" 由于两个外部引号引起的双引号
F3:H5 = ""X"" is an array of boolean results F3=X, F4=X etc.
F3:H5 = ""X"" 是一个布尔结果数组 F3=X, F4=X 等。
-- negative turns boolean to 0/1; another negative "back" to 1/0
-- 负数将布尔值变为 0/1;另一个负面“回到”到 1/0
IIf, Evaluate, SUMPRODUCT... Functions. Sorry, GIYF.
IIf、Evaluate、SUMPRODUCT...函数。对不起,GIYF。

