【问题标题】:Find list of words from a range if words exits multiple times如果单词多次退出,则从范围中查找单词列表
【发布时间】:2023-03-15 03:49:01
【问题描述】:

我有一个Sheet1 中的单词列表,我需要从Sheets("Sheet2").Range("A1:A7500")Range 的末尾一一匹配。每当单词匹配时,我需要在Sheet1 中对其进行处理。该词在Sheets("Sheet2").Range("A1:A7500") 中出现多次。

以下代码仅查找单词一次。我不明白哪里出了问题。

Sub XMAX()
Dim lrow As Long
Dim cel As Range
Dim oRng As Range: Set oRng = Sheets("Sheet2").Range("A1:A7500")
Dim oFoundRng As Range, oLastRng As Range


    lrow = Sheets("sheet1").Cells(Sheets("Sheet1").Rows.Count, "f").End(xlUp).Row
    '''''''''''''''Sheet1'''''''''''''''
    For Each cel In Range("f4:f" & lrow)

        If IsEmpty(cel.Value) = False Then

            Set oFoundRng = oRng.find(cel.Value)

            Do While Not oFoundRng Is Nothing

                    If UCase(oFoundRng.Offset(0, 1).Value) = "ISAAC" Then
                        Range("X" & cel.Row).Value = "X"
                    ElseIf UCase(oFoundRng.Offset(0, 1).Value) = "YO" Then
                        Range("V" & cel.Row).Value = "X"
                    ElseIf UCase(oFoundRng.Offset(0, 1).Value) = "JAN" Then
                        Range("U" & cel.Row).Value = "X"
                    Else
                        MsgBox oFoundRng.Value
                    End If


                Set oLastRng = oFoundRng                    
                Set oFoundRng = oRng.FindNext(cel.Value)   'Getting Error(1004) here "unable to get findnext property of the range class" 
                If oLastRng >= oFoundRng Then               
                    Exit Do                                 
                End If
            Loop

        Else
        End If

    Next

【问题讨论】:

  • If oLastRng >= oFoundRng Then的目的是什么?
  • @DisplayName 到 exit do 如果所有 'Cel.Value' 都已找到。我认为If oLastRng >= oFoundRng Then Exit Do 声明是错误的,因为它早早退出了do loop

标签: excel vba find range


【解决方案1】:

改变这一行

Set oFoundRng = oRng.FindNext(oFoundRng)

Set oFoundRng = oRng.FindNext

您搜索的不是单词,而是您之前找到的范围。实际上,您根本不需要将值传递给 .FindNext

你也必须改变这一行

If oLastRng >= oFoundRng Then

If oLastRng.Row >= oFoundRng.Row Then

因为第一行比较了值(这不是您想要做的,因为它总是评估为True)。您实际上想要比较行号。

另一方面,以下代码 sn-p 不起作用:

If UCase(oFoundRng.Offset(0, 1).Value) = "ISAAC" Then
    Range("X" & cel.Row).Value = "X"
ElseIf UCase(oFoundRng.Offset(0, 1).Value) = "ISAAC" Then
    Range("W" & cel.Row).Value = "X"

ElseIf 永远不会被触发,因为条件与初始If 条件相同。

你也不需要这两个语句:

Set oFoundRng = Nothing
Exit Do

他们都完成了同样的事情(打破循环),Exit Do 做得更有效。

【讨论】:

  • 听从了你的建议,更新了我的问题,现在看到Error (1004) unable to get findnext property of the range class
  • 我再次检查,您实际上不需要传递值,请尝试以下行:Set oFoundRng = oRng.FindNext
  • 进一步我认为If oLastRng >= oFoundRng Then Exit Do 声明是错误的,因为它提前退出do loop(没有第三次)。
  • 那是因为oLastRng >= oFoundRng 不比较行号,这是您真正想要的。该行将比较这些值,这将评估为True,因为两个单元格必须包含相同的字符串。试试这个:oLastRng.Row >= oFoundRng.Row。我也将其添加到我的答案中。
  • 为了将来参考,在查看自己的代码之前,您可能不想指出别人的代码是错误的。这似乎是阻止人们想要帮助你的好方法。
【解决方案2】:

你可能会在这之后(在 cmets 中解释):

Sub XMAX()
    Dim cel As Range
    Dim oRng As Range: Set oRng = Sheets("Sheet2").Range("A1:A7500")
    Dim oFoundRng As Range
    Dim firstAddress As String

    With Sheets("sheet1") ' reference "Sheet1" sheet
        With .Range("f4", .Cells(.Rows.Count, "f").End(xlUp)) ' reference referenced sheet column "F" range from row 4 down to last not empty one
            If WorksheetFunction.CountA(.Cells) > 0 Then ' if there's at least one not empty cell
                For Each cel In .SpecialCells(xlCellTypeConstants) ' loop through referenced range not empty cells

                    Set oFoundRng = oRng.Find(what:=cel.Value, LookIn:=xlValues, lookat:=xlWhole) ' always specify at least 'LookIn' and 'LookAt' parameters, or they will be set as per last 'Find()' usage (even from Excel UI!)
                    If Not oFoundRng Is Nothing Then ' if a match found
                        firstAddress = oFoundRng.Address ' store first matched cell address
                        Do
                            Select Case UCase(oFoundRng.Offset(0, 1).Value2)
                                Case "ISAAC"
                                    .Range("X" & cel.Row).Value = "X"
                                Case "YO"
                                    .Range("V" & cel.Row).Value = "X"
                                Case "JAN"
                                    .Range("U" & cel.Row).Value = "X"
                                Case Else
                                    MsgBox oFoundRng.Value
                            End Select

                            Set oFoundRng = oRng.FindNext(oFoundRng) ' search for next occurrence
                        Loop While oFoundRng.Address <> firstAddress ' exit do when hitting fisr found cell again
                    End If

                Next
            End If
        End With
    End With
End Sub

【讨论】:

  • @IbneAshiq,有什么反馈吗?
猜你喜欢
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多