简体   繁体   English

VBA 检查单元格值是否是数组的一部分

[英]VBA Check if a cell value is part of an array

Can somebody help on how to test if the value of the cell is part of a list?有人可以帮助测试单元格的值是否是列表的一部分吗? I have tried several things but nothing worked out :(我尝试了几件事,但没有任何效果:(

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("E4:E104")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub

Dim FI As Variant
FI = Sheets(1).Range("A1:A5").Value


With Target.Offset(0, -4)
.Value = Date
.NumberFormat = "dd.mm.yy"
End With

With Target.Offset(0, 5)
.FormulaR1C1 = "=R[-1]C+RC[-3]-RC[-2]"
End With

If Target = FI Then
    Target.Offset(0, 2).Locked = False
    Target.Offset(0, 3).Locked = True
ElseIf Target <> FI Then
    Target.Offset(0, 2).Locked = True
    Target.Offset(0, 3).Locked = False
End If

what do I have to add to see if the value f the cell is equal to one of the values of cells A1 to A5?我必须添加什么才能查看单元格的值 f 是否等于单元格 A1 到 A5 的值之一? thanks谢谢

I use this the function below to check if a value is in an array:我使用下面的函数来检查值是否在数组中:

Function CheckInArray(SearchVal As Variant, TestArray As Variant) As Boolean

    Dim icount As Long

    For icount = LBound(TestArray) To UBound(TestArray)

        If TestArray(icount, 1) = SearchVal Then

            CheckInArray = True
            Exit Function

        End If

    Next icount

    CheckInArray = False

End Function

The function returns True if your SearchVal is in the array, False if it isn't.如果您的SearchVal在数组中,则该函数返回True否则返回False You could add a line like:您可以添加如下一行:

If CheckInArray(Target.value, FI) then

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

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