可悲的是,我不是很精通VBA编码,如果我在网上找到了示例代码,就很难了。我的问题是代码似乎不喜欢很大的列号。当“ LookupRange As Range”变为A1:A10000时,VBA将不再正常工作。例如代码
=singlecellextract(J1,D1:E5,2)
工作正常,但
=singlecellextract(J1,D1:E12000,2)
返回#VALUE!错误
这是我用于该功能的VBA代码
Function SingleCellExtract(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer)
Dim i As Long
Dim result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = Lookupvalue Then
result = result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
Next i
SingleCellExtract = Left(result, Len(result) - 1)
End Function
此代码是否有可能与大量的列数字小工具一起使用,或者更好地与A:A之类的输入一起使用?非常感谢。
编辑:我将double值更改为long值,但似乎并没有影响结果
答案 0 :(得分:0)
只要没有错误,该公式似乎就可以正常工作(尽管很慢)。 以下调整将使其忽略错误。
Function SingleCellExtract(Lookupvalue As String, _
LookupRange As Range, _
ColumnNumber As Integer)
Dim i As Long
Dim result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If Not (IsError(LookupRange.Cells(i, 1)) Or _
IsError(LookupRange.Cells(i, ColumnNumber))) Then
If LookupRange.Cells(i, 1) = Lookupvalue Then
result = result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
End If
Next i
SingleCellExtract = Left(result, Len(result) - 1)
End Function