您需要将该函数的返回值存储在某处,然后返回对应于最后一个位置的值。
我假设您正在使用t
遍历名称。
添加这些行:
dim BigPos as long
dim BigLoc as long
dim CurrentPos as long
然后更改您的循环以实际记住它在字符串中找到的最远的名称
BigPos=0
BigLoc=0 ' set to zero, so no old checks can be found
for t=lbound(MyNames) to ubound(MyNames)
currentpos=InStrRev(Range("t" & r).Value, MyNames(t), -1, vbTextCompare)
if currenpos>bigpos then
'found one of the names *and*
'it's further along than the previous found name
bigpos=currentpos 'remember where in the string we found the text
bigloc=t 'and remember which name
end if
next t 'check them all, so there's no exit out of the for loop
然后你可以显示结果:
if bigloc=0 then
msgbox "No names found"
else
msgbox "Name " & MyNames(bigloc) & " found at character " & bigpos
end if