|
楼主 |
发表于 2019-9-7 19:09
|
显示全部楼层
本帖最后由 YZC51 于 2019-9-8 15:31 编辑
'//本程序参考 兰色幻想 老师的 Wlookup 函数制作。在此致谢!2019-09-08 05:23:13
模拟微软最新 Xlookup 函数
Function Xlookup(v, vY, vh, Optional m = 0, Optional n = 1) '匹配未完成
Dim arr, arr1, arr2()
Dim k As Integer
On Error Resume Next
If Len(v) = 0 Then v = 0
arr = vY
arr1 = vh
If UBound(arr1) = 1 Then
arr1 = Application.Transpose(arr1)
arr = Application.Transpose(arr)
End If
ReDim arr2(1 To 1)
For x = 1 To UBound(arr1)
If m = 2 Then
flg = arr(x, 1) Like v '通配符匹配
ElseIf m = 3 Then
flg = InStr(arr(x, 1), v) '包含匹配
Else
flg = (arr(x, 1) = v) '精确匹配
End If
If flg And n = 1 Then
If UBound(arr1, 2) > 1 Then
Xlookup = arr1(x, 1)
With Application.ThisCell
For j = 1 To UBound(arr1, 2) '自动填充
If .Offset(, j) = "" Then s = Null Else s = "*"
.Offset(, j).Replace s, arr1(x, j + 1)
Next j
End With
End If
Xlookup = arr1(x, 1)
Exit Function
Else
If arr(x, 1) = v Then
k = k + 1
ReDim Preserve arr2(1 To k)
arr2(k) = arr1(x, 1)
End If
End If
Next x
If Abs(m) = 1 Then
Xlookup = JS(v, vY, vh, m)
Else
If n = 0 Then Xlookup = Join(arr2, ",")
If n < 0 Then Xlookup = arr2(k)
If n > 0 Then Xlookup = arr2(n)
End If
End Function
Function JS(J1, R1, R2, m) '取上下接近值
Dim Jarr1, Jarr2
Dim x
Jarr1 = R1
Jarr2 = R2
For x = 1 To UBound(Jarr1)
If x + 1 > UBound(Jarr1) Then
JS = Jarr2(x, 1)
Exit Function
ElseIf J1 >= Jarr1(x, 1) And J1 < Jarr1(x + 1, 1) Then
If m = -1 Then JS = Jarr2(x, 1) Else JS = Jarr2(x + 1, 1)
Exit Function
End If
Next x
End Function
|
评分
-
3
查看全部评分
-
|