简体   繁体   English

Excel VBA 如果 range.value = 某物,则

[英]Excel VBA If range.value = something then

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

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

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

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
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; -- 负数将布尔值变为 0/1; another negative "back" to 1/0另一个负面“回到”到 1/0

IIf, Evaluate, SUMPRODUCT... Functions. IIf、Evaluate、SUMPRODUCT...函数。 Sorry, GIYF.对不起,GIYF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM