VBA 中的查找函数不是查找下一个

发布于 2025-01-11 17:48:49 字数 997 浏览 0 评论 0原文

我正在尝试写一个宏。我所在的部分是我的参考 ID 可以在一列中多次找到。它位于文本框中,但单元格只会有一次参考 ID。该参考 ID 最多可以匹配多行。我的宏的其余部分工作正常。当我尝试使用 find 或 findnext 时,我就搞砸了。我有一个计数器,用于查找我的参考 ID 在我分配给范围变量的列范围 I:I 中出现的次数。我在 do while 循环中从该计数器中减去并在其中使用 find next 。问题是在找到前两个唯一值后,它只会卡在找到的第二个值上并继续填充该值。这是我到目前为止的代码。

    If Counter > 0 Then
        
        'Uses cellFound as a range set to our original find
        
        Set cellFound = DidIFind.Find(valueToSearch)
        
        'Keeps going till we find all the values that the counter ticks down
        
        Do While Counter > 0

            Set cellFound = cellFound.FindNext
            Set cellFound = DidIFind.Find(valueToSearch)
            'Marks row position of new duplicate find so we can add to ID string
            
            Rw = cellFound.Row
            ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
            
            'Counter ticks down by 1
            Counter = Counter - 1
        Loop
    End If

I am trying to write a macro. I am at the part where my reference ID can be found multiple times in a column. It is in a text box but the cell will only have the reference ID once. That reference ID can match up to multiple rows. I have the rest of the macro working fine. It's when I try to use find or findnext that I am messing up. I have a counter that looks up how many times my reference ID showed up in the column range I:I which I have assigned to a range variable. I subtract from that counter in a do while loop and use find next in it. The problem is after finding the first two unique values it just gets stuck on the second value it found and keeps populating that. This is my code so far.

    If Counter > 0 Then
        
        'Uses cellFound as a range set to our original find
        
        Set cellFound = DidIFind.Find(valueToSearch)
        
        'Keeps going till we find all the values that the counter ticks down
        
        Do While Counter > 0

            Set cellFound = cellFound.FindNext
            Set cellFound = DidIFind.Find(valueToSearch)
            'Marks row position of new duplicate find so we can add to ID string
            
            Rw = cellFound.Row
            ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
            
            'Counter ticks down by 1
            Counter = Counter - 1
        Loop
    End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2025-01-18 17:48:49

Do LoopFindNext

' This is usually handled without any counter but no harm done.
' I'm assuming 'Counter = Application.CountIf(DidIFind, valueToSearch)'.
If Counter > 0 Then
    ' if all this is in a loop you need:
    'ConcatString = ""
    Set CellFound = DidIFind.Find(valueToSearch)
    Do
        Rw = CellFound.Row
        ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
        Counter = Counter - 1
        If Counter = 0 Then Exit Do
        Set CellFound = DidIFind.FindNext(CellFound)
    Loop
    ' remove leading delimiter (", ")
    ConcatString = Right(ConcatString, Len(ConcatString) - 2)
End If

Do Loop vs FindNext

' This is usually handled without any counter but no harm done.
' I'm assuming 'Counter = Application.CountIf(DidIFind, valueToSearch)'.
If Counter > 0 Then
    ' if all this is in a loop you need:
    'ConcatString = ""
    Set CellFound = DidIFind.Find(valueToSearch)
    Do
        Rw = CellFound.Row
        ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
        Counter = Counter - 1
        If Counter = 0 Then Exit Do
        Set CellFound = DidIFind.FindNext(CellFound)
    Loop
    ' remove leading delimiter (", ")
    ConcatString = Right(ConcatString, Len(ConcatString) - 2)
End If
空袭的梦i 2025-01-18 17:48:49

下面是如何使用 Range.FindNext 的一个很好的示例。

Excel 将使用之前的 Range.Find LookInLookAt 值。因此,需要对它们进行设置以避免任何形式的歧义。

FindCells

我编写了这个函数来返回所有找到的单元格的并集。

Function FindCells(Source As Range, What As Variant, Optional LookIn As XlFindLookIn = xlValues, Optional LookAt As XlLookAt = xlWhole) As Range
    Dim Map As New Collection
    Dim Target As Range
    Dim FirstCellAddress As String
    Dim AllCells As Range
    
    Set Target = Source.Find(What:=What, LookIn:=LookIn, LookAt:=LookAt)
    If Target Is Nothing Then Exit Function
    
    FirstCellAddress = Target.Address
    Set AllCells = Target

    Do
        Set Target = Source.FindNext(Target)
        If FirstCellAddress <> Target.Address Then
            Set AllCells = Union(AllCells, Target)
        End If
    Loop While FirstCellAddress <> Target.Address
    
    Set FindCells = AllCells
End Function

使用

Sub Usage()
    Const What As String = "Bangalore"
    Const Col As Long = 3
    Dim Source As Range
    Set Source = Sheet1.UsedRange.Columns(1)
    
    Dim Result As Range
    Set Result = FindCells(Source, What)
    
    If Not Result Is Nothing Then
        
        Dim Cell As Range
        Dim Text As String
        
        For Each Cell In Result
            Text = Text & ", " & Cell.EntireRow.Columns(Col).Value
        Next
        Text = Mid(Text, 3)
        Debug.Print Text
    End If
    
End Sub

数据

Data

结果

Here is a good example of how to use Range.FindNext.

Excel will use the previous Range.Find LookIn and LookAt values. For this reason they need to be set to avoid any kind of ambiguity.

FindCells

I wrote this function to return the Union of all the found cells.

Function FindCells(Source As Range, What As Variant, Optional LookIn As XlFindLookIn = xlValues, Optional LookAt As XlLookAt = xlWhole) As Range
    Dim Map As New Collection
    Dim Target As Range
    Dim FirstCellAddress As String
    Dim AllCells As Range
    
    Set Target = Source.Find(What:=What, LookIn:=LookIn, LookAt:=LookAt)
    If Target Is Nothing Then Exit Function
    
    FirstCellAddress = Target.Address
    Set AllCells = Target

    Do
        Set Target = Source.FindNext(Target)
        If FirstCellAddress <> Target.Address Then
            Set AllCells = Union(AllCells, Target)
        End If
    Loop While FirstCellAddress <> Target.Address
    
    Set FindCells = AllCells
End Function

Usage

Sub Usage()
    Const What As String = "Bangalore"
    Const Col As Long = 3
    Dim Source As Range
    Set Source = Sheet1.UsedRange.Columns(1)
    
    Dim Result As Range
    Set Result = FindCells(Source, What)
    
    If Not Result Is Nothing Then
        
        Dim Cell As Range
        Dim Text As String
        
        For Each Cell In Result
            Text = Text & ", " & Cell.EntireRow.Columns(Col).Value
        Next
        Text = Mid(Text, 3)
        Debug.Print Text
    End If
    
End Sub

Data

Data

Result

Result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文