简体   繁体   English

如何使用vba搜索单元格中是否存在数组中的所有值

[英]How to search if all the values in an array in present in a cell, using vba

I have an array of names stored in column a in Sheet 2. Say John, Smith, Bob, Peter, William. 我在工作表2的a列中存储了一组名称。说约翰,史密斯,鲍勃,彼得,威廉。 I need to check a cell in Sheet 1 and see what name is comes last in that cell. 我需要检查工作表1中的一个单元,然后查看该单元中最后一个名字是什么。

Suppose the content in Sheet1, A1 is "William came home early. Smith rang the bel and Bob opened the door to see if that was Peter". 假设Sheet1中A1的内容是“威廉早早回家。史密斯敲响了钟声,鲍勃打开了门,看看那是否是彼得”。 In this, my requirement are, which all names contained in this sentence in A1 cell and whose name came last in this. 在此,我的要求是,该句子在A1单元格中包含的所有名称,其名称排在最后。 Obviously it's Peter and I need it to be returned using vba. 显然是Peter,我需要使用vba将其返回。

I have written a macro using InStrRev function and using MyNames array which contains the names. 我已经使用InStrRev函数和包含名称的MyNames数组编写了一个宏。 However it searches the name in array in descending order only. 但是,它仅以降序搜索数组中的名称。

InStrRev(Range("t" & r).Value, MyNames(t), -1, vbTextCompare)

You could use a function to return the last used name. 您可以使用函数返回上次使用的名称。

The function works as a Worksheet function or VBA as it returns a String(name) 该函数用作工作表函数或VBA,因为它返回一个String(name)

Function LastUsedName(rng As Range) As String

    Dim names As Variant
    names = Sheets(2).Range("A1:A" & Sheets(2).Range("A" & Rows.Count).End(xlUp).Row)

    Dim cell As Range, i As Long, j As Long
    Dim arr As Variant
    arr = Split(rng, Chr(32))

    For j = UBound(arr) To LBound(arr) Step -1
        For i = LBound(names) To UBound(names)
            If names(i, 1) = arr(j) Then
                LastUsedName = names(i, 1)
                Exit Function
            End If
        Next i
    Next j

End Function

so 所以

Sheet1 cell A1 Sheet1单元格A1

在此处输入图片说明

Sheet2 column A Sheet2 A栏

在此处输入图片说明

if you stick the function name in any cell then 如果将函数名称粘贴在任何单元格中,则

在此处输入图片说明

you need to store the return value from that function somewhere, and then return the value that corresponds to the last position. 您需要将该函数的返回值存储在某个位置,然后返回与最后一个位置相对应的值。

I'm assuming you're using t to loop through the names. 我假设您正在使用t来遍历名称。

add in these lines: 在这些行中添加:

dim BigPos as long
dim BigLoc as long
dim CurrentPos as long

then alter your loop to actually remember which was the name it found furthest along the string 然后更改您的循环以实际记住沿字符串找到最远的名称

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

then you can display the result: 然后您可以显示结果:

if bigloc=0 then 
    msgbox "No names found"
else
    msgbox "Name " & MyNames(bigloc) & " found at character " & bigpos
end if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM