首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA循环selection.find

VBA循环selection.find
EN

Stack Overflow用户
提问于 2021-12-21 09:08:20
回答 1查看 54关注 0票数 -1

我想在另一个工作表中循环或找到多个值。即使在我do..loop代码之后,我的代码也不能工作。

代码语言:javascript
复制
For i = 1 To lastrowBAU

        Worksheets(fname).Range("A1:A" & lastrowsheet).Select
        
        Do Until Cell Is Nothing
         Set Cell = Selection.find(What:=ThisWorkbook.Worksheets("BAU").Range("A" & i).Value, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, _
                MatchCase:=False)
            
                 If Not Cell Is Nothing Then
        
                    Cell.Activate
                    ActiveCell.Copy
                    ActiveCell.Insert Shift:=xlShiftDown
                    
                             ActiveCell.Offset(1, 0).Select
                             
                    Selection.Replace What:=ThisWorkbook.Worksheets("BAU").Range("A" & i).Value, _
                    replacement:=ThisWorkbook.Worksheets("BAU").Range("B" & i).Value, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
                    Set Cell = Worksheets(fname).Range("A1:A" & lastrowsheet).FindNext(Cell)
                    
                End If
       Loop
  Next i
EN

回答 1

Stack Overflow用户

发布于 2021-12-21 12:17:45

您需要在进入循环之前设置单元格。

代码语言:javascript
复制
 Set cell = rngSrc.Find(sA, LookIn:=xlFormulas, LookAt:=xlPart, _
                      After:=rngSrc.Cells(rngSrc.Cells.Count), SearchOrder:=xlByRows, MatchCase:=False)
        
 If Not cell Is Nothing Then

但是,您还需要通过检查搜索是否返回到第一个找到的循环来避免无休止的循环。

代码语言:javascript
复制
Option Explicit

Sub macro1()

    Dim ws As Worksheet, wsBAU As Worksheet
    Dim cell As Range, rngSrc As Range
    Dim fname As String, lastrow As Long, lastrowBAU As Long
    Dim i As Long, n As Long, first As String
    Dim sA As String, sB As String
    
    fname = "Sheet1"
    
    With ThisWorkbook
        Set ws = .Sheets(fname)
        Set wsBAU = .Sheets("BAU")
    End With
    
    With ws
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rngSrc = .Range("A1:A" & lastrow)
    End With
    
    With wsBAU
        lastrowBAU = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
     With ws
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rngSrc = .Range("A1:A" & lastrow)
    End With
    
    ' search and replace
    Application.ScreenUpdating = False
    For i = 1 To lastrowBAU
        sA = wsBAU.Cells(i, "A")
        sB = wsBAU.Cells(i, "B")
        
        Set cell = rngSrc.Find(sA, LookIn:=xlFormulas, LookAt:=xlPart, _
                      After:=rngSrc.Cells(rngSrc.Cells.Count), SearchOrder:=xlByRows, MatchCase:=False)
        
        If Not cell Is Nothing Then
        
            first = cell.Address
            Do
                ' insert cell above
                cell.Insert xlDown
                cell.Offset(-1).Value2 = cell.Value2
                cell.Value2 = Replace(cell.Value2, sA, sB)
                ' expand search range
                n = n + 1
                Set rngSrc = ws.Range("A1:A" & lastrow + n)
                ' find next
                Set cell = rngSrc.FindNext(cell)
            Loop While cell.Address <> first
            
        End If
    Next
    Application.ScreenUpdating = True
    MsgBox n & " replacements", vbInformation
    
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70432930

复制
相关文章

相似问题

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