对于我的一生,我不知道为什么这不起作用。它给了我#VALUE错误。
我使用ActiveSheet是因为我将其放在许多不同的工作表上,并且我不想为此在功能中添加字段。
LookupRange旨在在ActiveSheet上查找包含数据的最后一行。
我的查找值从B5开始并无限期扩展,所需的匹配项位于O列(第15列)中。
Function EmailConcat(LookupValue As String)
Application.Volatile
Dim i As Long
Dim Result As String
Dim LookupSheet As Worksheet
Dim LookupRange As Range
Set LookupSheet = Application.ActiveSheet
LookupRange = LookupSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
For i = 5 To LookupRange.Rows.Count
If LookupSheet.Cells(i, 2) = LookupValue Then
Result = Result & LookupSheet.Cells(i, 15) & "; "
End If
Next i
EmailConcat = Left(Result, Len(Result) - 2)
End Function
答案 0 :(得分:1)
使用ThisCell
确保结果准确,然后将查询列读入数组以提高性能:
Function EmailConcat(LookupValue As String)
Application.Volatile
Dim vals, rv, i As Long, sep As String
If LookupValue <> "" Then
With Application.ThisCell.Worksheet
vals = .Range(.Range("B5"), .Cells(.Rows.Count, 2).End(xlUp))
For i = 1 To UBound(vals, 1)
If vals(i, 1) = LookupValue Then
rv = rv & sep & .Cells(4 + i, 15).Value
sep = "; "
End If
Next i
End With
End If
EmailConcat = rv
End Function
答案 1 :(得分:0)
在不使用限定符的情况下使用范围或单元格等时,它们指的是ActiveSheet
中的ActiveWorkbook
。
Function EmailConcat(LookupValue As String)
Application.Volatile
Const cFirst As String = "B5"
Const cCol As Variant = "O"
Dim i As Long
Dim Result As String
Dim LastRow As Long
LastRow = Cells.Find("*", , xlFormulas, xlWhole, xlByRows, xlPrevious).Row
For i = Range(cFirst).Row To LastRow
If Cells(i, Range(cFirst).Column) = LookupValue Then
Result = Result & Cells(i, cCol) & "; "
End If
Next i
EmailConcat = Left(Result, Len(Result) - 2)
End Function