标签:VBA,自定义函数
如下图1所示,查找列A中值为“figs”的行,并返回该行中内容为“X”的单元格对应的该列中首行单元格的内容,即图1中红框所示的内容。
图1
在单元格B20中输入公式:
=lookupFruitColours(A20,"X",A2:J17,A1:J1)
这个公式使用了自定义函数lookupFruitColours。这个自定义函数的代码如下:
Option Compare Text
Function lookupFruitColours(ByVal lookup_value As String, _
ByVal intersect_value As String, _
ByVal lookup_vector As Range, _
ByVal result_vector As Range) As String
Application.Volatile
Dim varLookupVector As Variant
Dim varResultVector As Variant
Dim lngIndexRows As Long
Dim intIndexColumns As Integer
Dim result_set As String
varLookupVector = lookup_vector
varResultVector = result_vector
For lngIndexRows = LBound(varLookupVector, 1) To UBound(varLookupVector, 1)
If Trim(varLookupVector(lngIndexRows, 1)) = Trim(lookup_value) Then
For intIndexColumns = LBound(varResultVector, 2) To UBound(varResultVector, 2)
If varLookupVector(lngIndexRows, intIndexColumns) = Trim(intersect_value) Then
result_set = varResultVector(1, intIndexColumns) & "," & result_set
End If
Next intIndexColumns
End If
Next lngIndexRows
lookupFruitColours = Left(result_set, Len(result_set) - 1)
End Function
其中,参数lookup_value代表要在指定区域第一列中查找的值,参数intersect_value代表行列交叉处的值,参数lookup_vector代表指定的查找区域,参数result_vector代表返回值所在的区域。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有