56、自定义函数的可选参数+多重条件模糊引用。
QJcfIRMB.rar
(8.54 KB, 下载次数: 150)
代码:
Function H_Find(rng As Range, rng1 As Range, x1, Optional x2, Optional x3, Optional x4, Optional x5) As String
Dim s As String '可选参数,共4个,x1是比选的
Dim i%
Application.Volatile '每次单元格改动时,都会自动更新这个自定义函数的值
With rng
If rng.Count = 65536 Then H_Find = "ERROR": Exit Function '不要用整列做查找范围
For i = 1 To .Count
If InStr(1, .Cells(i), x1) = 0 Then GoTo line1 '假如不包含x1这个条件的话,就直接调整到ling1行,就执行next循环
If Not IsMissing(x2) Then '假如自定义函数中使用了可选参数,则
If InStr(1, .Cells(i), x2) = 0 Then GoTo line1 '查看这个参数是否在单元格里出现,如果没有出现则跳到line1行
End If
If Not IsMissing(x3) Then '同上
If InStr(1, .Cells(i), x3) = 0 Then GoTo line1
End If
If Not IsMissing(x4) Then '同上
If InStr(1, .Cells(i), x4) = 0 Then GoTo line1
End If
If Not IsMissing(x5) Then '同上
If InStr(1, .Cells(i), x5) = 0 Then GoTo line1
End If
s = s & IIf(Len(s) = 0, "", ",") & rng1.Cells(i) '最终没有跳转就说明全部5个条件都符合,则链接到一个字符串s里面
line1:
Next
End With
H_Find = s '给自定义函数赋值
End Function
|