vba - '.Find'/'.FindNext' 重复查找相同单元格或返回错误

标签 vba excel

我有一个医学术语列表(F 列)及其关联的数字代码(G 列),我需要在 B 列的列表中的 F 列中找到医学术语,并将该术语的关联代码放入 C 列.

我的电子表格的简化版本的图像:

simplified version of my spreadsheet

我希望电子表格在代码运行后看起来像这样:

after the code is run

我的问题是获取代码来查找列表中的下一个匹配项。我在图像中的示例是医学术语:异常步态。您可以看到 B 列(第一个和最后一个单元格)中有两个匹配项。我的代码是根据 Microsoft 中的示例修改的。以及[许多论坛一直推荐作为资源的另一个网站][3]。但是,无论我尝试修改第二个“查找”命令多少次,我总是会遇到以下错误之一:

  1. 无法获取 Range 类的 FindNext 属性
  2. 类型不匹配错误
  3. Find 函数重复查找同一单元格
  4. Find 函数找到第一个单元格,但它永远找不到下一个单元格并通过 End If 退出。

    Sub Match2Cohort()
    Dim Phenotype, FindMe, FoundinList As Range
    Dim LRp, LastRow, i As Long
    Dim FirstMatch As String
    
    LRp = Cells(Rows.Count, 2).End(xlUp).Row
    LastRow = Cells(Rows.Count, 6).End(xlUp).Row
    Set Phenotype = Range("B1:b" & LRp)
    Set Terms = Range("F1:f" & LastRow)
    
    For i = 18 To LastRow
        FindMe = Cells(i, 6).Value
        Set FoundinList = Phenotype.Cells.Find(What:=FindMe, LookAt:=xlWhole)
        On Error Resume Next
    
        If Not FoundinList Is Nothing Then
            FirstMatch = FoundinList.Row
            Do
    'This loop allows me to combine multiple medical codes into the same cell.                    
                If IsEmpty(FoundinList.Offset(0, 1)) = True Then
                        FoundinList.Offset(0, 1) = Cells(i, 7).Value
                Else: FoundinList.Offset(0, 1) = FoundinList.Offset(0, 1).Value & "/" & Cells(i, 7).Value
                FoundinList.Offset(0, 1).Select
                End If
    
    'This is the code that is not working and all of the variations I've tried:
        With Phenotype
            Set FoundinList = .FindNext(FindMe)
            Set FoundinList = .FindNext(FindMe, After:=ActiveCell)
            Set FoundinList = .FindNext(After:=ActiveCell)
        End With
    
            Set FoundinList = Phenotype.FindNext(What:=FindMe, After:=ActiveCell, LookAt:=xlWhole)
            Set FoundinList = Phenotype.Find(What:=FindMe, After:=ActiveCell, LookAt:=xlWhole)
            Set FoundinList = Phenotype.FindNext(After:=FoundinList)
            Set FoundinList = Phenotype.FindNext(What:=FindMe, After:=FoundinList, LookAt:=xlWhole)
            Set FoundinList = Phenotype.Find(What:=FindMe, After:=FoundinList, LookAt:=xlWhole)
    
            Loop While FirstMatch <> FoundinList.Row
        End If
    Next i
    
    End Sub
    

此时,我已经尝试了我能想到的一切以及我在网上找到的所有内容,但只是不知道下一步该尝试什么。

最佳答案

这是解决您的问题的有效解决方案,不使用 .Find.FindNext 方法。

Sub Match2Cohort()
    Dim i&, k&, TTmp$, PTmp$, p, t
    t = [f1].CurrentRegion.Resize(, 2)
    With ActiveSheet
        p = [b1].Resize(.Cells(.Rows.Count, "b").End(xlUp).Row, 2)
    End With
    For i = 1 To UBound(t)
        TTmp = LCase$(Replace(t(i, 1), " ", ""))
        For k = 1 To UBound(p)
            PTmp = "," & LCase$(Replace(p(k, 1), " ", "")) & ","
            If InStr(PTmp, "," & TTmp & ",") Then
                PTmp = p(k, 2) & "/" & t(i, 2)
                If Left$(PTmp, 1) = "/" Then PTmp = Mid$(PTmp, 2)
                p(k, 2) = PTmp
            End If
        Next
    Next
    [b1].Resize(UBound(p), UBound(p, 2)) = p
End Sub

关于vba - '.Find'/'.FindNext' 重复查找相同单元格或返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33306673/

相关文章:

excel - 在 Excel 中选择特定单元格

java - JasperReport 报表中的 Excel 单元格格式

excel - 循环遍历 VBA 中的单元格

vba - 如何将工作表 A 保存到文件夹 A 并将工作表 B 保存到文件夹 B?

excel - 找到最后一行空的,插入图片(代码无法识别已经包含图像的单元格)

sql - 使用 SQL 查询没有标题行的 Excel 工作表

vba - Excel/VBA : Check if Cell Reference returns 0

vba - 将多张工作表中的行复制到一张工作表中

excel - 如果列包含给定数字 X 次,则突出显示单元格

VBA - 如何将集合添加到集合的集合中