首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个不同的单元格中查找相同的字符串

在两个不同的单元格中查找相同的字符串
EN

Stack Overflow用户
提问于 2018-12-10 01:02:13
回答 1查看 56关注 0票数 0

我试图在两个不同的单元格中查找字符串值,但无济于事。也许和搜索方向有关。代码在第一个单元格中查找字符串,但在第二个单元格中找不到(单元格不相邻)??

请参阅代码摘录:

代码语言:javascript
复制
Dim aCell As Range, bCell, where1, where2
    lastarr = Worksheets("Locations").Range("C50").End(xlUp).Row - 1
    If lastarr < 1 Then GoTo end4
    Dim whatar()
    ReDim Preserve whatar(lastarr)
    where1 = .Cells(Application.Caller.Row, 4).Address _
             (RowAbsolute:=False, ColumnAbsolute:=False)
    where2 = .Cells(Application.Caller.Row, 9).Address _
             (RowAbsolute:=False, ColumnAbsolute:=False)
    For i = 1 To lastarr
        whatar(i) = Trim(Worksheets("Locations").Cells(i + 1, 3))
        Set aCell = .Range(where1).Find(What:=whatar(i), LookIn:=xlValues, _
            LookAt:=xlPart, MatchCase:=False)
        Set bCell = .Range(where2).Find(What:=whatar(i), LookIn:=xlValues, _
            LookAt:=xlPart, MatchCase:=False)
        rr = Application.Caller.Row
        If Not bCell Is Nothing Then Stop
        'If rr = 439 And i = 3 Then Stop
        If Not aCell Is Nothing Or Not bCell Is Nothing Then  
            gekry = True   '======= trigger!! =========
            'Stop
        End If
    Next
EN

回答 1

Stack Overflow用户

发布于 2018-12-10 10:36:19

我试着遵循你的代码和逻辑,但没能坚持到底。如果您正在寻找给定字符串的多个实例,您可以采用我在这种情况下通常使用的样板代码:

代码语言:javascript
复制
Dim strFirstFind As String
Dim cll As Range, rng As Range

With ActiveSheet.Range("A1:X100")
    Set cll = .Find("X", LookIn:=xlValues)
    If Not cll Is Nothing Then
        Set rng = cll
        strFirstFind = cll.Address
        Do
            Set rng = Union(rng, cll)
            .Range(cll.Address).Activate
            Set cll = .FindNext(cll)

        Loop While Not cll Is Nothing And cll.Address <> strFirstFind
    End If

    Debug.Print rng.Address

End With
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53694617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档