我有一个简短的用户定义函数,给定一个字符串和列中的一组可能的子字符串,将返回找到的第一个子字符串的位置。
我被要求用非 VBA 公式替换 UDF。
Option Explicit
Public Function Locate(s As String, rng As Range) As Variant
Dim pos As Long, p As Long, cell As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Locate = CVErr(xlErrNA)
pos = 999999
For Each cell In rng
p = InStr(s, cell.Text)
If p > 0 Then
pos = wf.Min(pos, p)
End If
Next cell
If pos <> 999999 Then Locate = pos
End Function
这是一个例子:
子字符串在列F中。该函数找到b和q并返回 5,因为q先出现。
我有一个适用于特定实例的公式:
=MIN(FIND({"a","b","c","d","q"},A1 & "abcdq",1))
我一直在尝试找到一种方法来替换{"a","b","c","d","q"}
and"abcdq"
引用一系列单元格,但我很难过。
使用连接: