當我打電話從Excel的功能(在細胞):爲什麼我的電子表格函數的行爲與從代碼中調用時行爲不同?
=allVlookup(O24,A:D,3,"")
VS通過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
我茫然地解釋爲什麼發生這種情況。
我該怎麼做才能使輸出結果一致?
我可以忍受這是一個錯誤。討厭,但是,很高興知道。 – enderland