当我从 Excel(在单元格中)调用函数时:
=allVlookup(O24,A:D,3,"")
与通过 vba 对比
MsgBox allVlookup(Range("O24"), Range("A:D"), 3, "")
我得到不同的结果。当从 Excel 调用时,我只得到第一个匹配,但是当从具有相同参数的 vba 测试子调用时(除了添加Range
到参数以允许子运行),我得到完整的结果(不止一个)。
我正在使用的功能是:
Public Function allVlookup(lookupRange As Range, tableRange As Range, colIndex As Integer, Optional delimiter As String = "") As String
Dim c As Range
Dim firstAddress As String
'MsgBox tableRange.Address ' this is correct
'With Sheets(4).Range("A1:C12").Columns(1)
'With Range("A1:C12").Columns(1)
'this doesn't allow things to work right either (???)
'Set tableRange = Range("A:D")
'Set lookupRange = Range("O24")
'search only the first column for matches
With tableRange.Columns(1)
Set c = .Find(what:=lookupRange.Value, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
'add the delimiter
If (allVlookup <> "") Then
allVlookup = allVlookup + delimiter
End If
'append value to previous value
allVlookup = allVlookup + c.Offset(0, colIndex).Value
Set c = .FindNext(c)
'exit conditions
'no match found
If (c Is Nothing) Then
Exit Do
'we're back to start
ElseIf (c.Address = firstAddress) Then
Exit Do
End If
Loop
End If
End With
End Function
我不知道为什么会发生这种情况。
我该怎么做才能使输出相同?