我正在尝试编写Excel VBA函数。用户输入2个参数,该功能检查第一个是否在第二个参数中,返回TRUE
或FALSE
。我已经得到它作为一个子程序,但不是一个函数。我错过了什么?Excel VBA:查找范围内的对象
这可能是非常基本的,但它一直给我一些问题。
谢谢!
**我意识到,我可以使用,而不是一个集合的阵列,但我更喜欢像这样如果可能的话
编辑:为了描述更详细一点的问题:我期待确定如果在list
范围内找到input_cell
中的值,则不在input_cell
位于list
范围内。
当我进入这两个值,它会返回一个错误#VALUE!
Function INRANGE(input_cell As Range, list As Range) As Boolean
Dim coll As New Collection
'*******Add items to coll********'
'Find size of collection
Dim LastRow As Long
LastRow = list.Rows.Count
'Cells(Rows.Count, input_cell.Column).End(xlUp).Row
'Add rows to collection
Dim i As Integer
For i = list.Row To LastRow
coll.Add Cells(i, list.Column).Value
Next i
'*******Search collection********'
Dim Current_Cell As Variant
Dim isInList As Boolean
isInList = False
Dim k As Integer 'Count of loop through list
For k = list.Row To LastRow
If input_cell = coll(k) Then
isInList = True
End If
Next k
INRANGE = isInList
End Function
它看起来像你试图从函数本身使用该功能。您还声明了两个范围参数,但不提供它们。你不应该告诉函数等于false。您需要将参数提供给函数以从中返回布尔值(如您声明的那样)。 – Brian
您是否考虑过使用内建的'Intersect'函数,该函数根据两个范围的交集返回“TRUE/FALSE”。根据您的文章,我认为'Intersect'可以在单行代码中更高效地完成您所需要的内容。 – user3561813
您是否试图确定input_cell是否在列表中(例如C2位于C1:C10内部)或者您是否试图确定input_cell中的值是否在列表中找到(例如,5在{3,2,6,7 ,5,10,11} – OldUgly