searching for multiple occurrence of a text and changing another text in vba

女生的网名这么多〃 提交于 2020-06-18 11:17:40

问题


I am pretty new at vba. I am searching for a string in excel can also be multiple occurrence and then for each occurrence changing another text if this string is found. but my code is going into infinite loop. How can i achieve that? Here is my code:

Private Sub PRODA_Replace_IfNDM_Click()
    Dim FindWord As String, Found As Range
    Dim firstAddress As String
    Dim rslt As Range
    FindWord = "/*_NDM*"
    Set Found = Sheets("PRODA").Cells.Find(What:=FindWord, _
                                           LookIn:=xlValues, _
                                           lookAt:=xlPart, _
                                           searchOrder:=xlByRows, _
                                           searchDirection:=xlNext, _
                                           MatchCase:=False)
    If Not Found Is Nothing Then
        firstAddress = Found.Address
        Do
            Set rslt = Sheets("PRODA").Cells.Find(What:="OWNER:*", _
                                                  After:=Found, _
                                                  LookIn:=xlValues, _
                                                  lookAt:=xlPart, _
                                                  searchOrder:=xlByRows, _
                                                  searchDirection:=xlNext, _
                                                  MatchCase:=False)
            Sheets("PRODA").Cells.Replace What:=rslt, _
                                          Replacement:="OWNER:chrndm", _
                                          lookAt:=xlPart, _
                                          searchOrder:=xlByRows, _
                                          MatchCase:=False, _
                                          searchFormat:=False, _
                                          ReplaceFormat:=False
            MsgBox Found
            If Found Is Nothing Then
                GoTo endFinding
            End If
            Set Found = Cells.FindNext(After:=Found)
        Loop While Not Found Is Nothing And firstAddress <> Found.Address
    End If
endFinding:
    MsgBox "changed successfully"
End Sub

回答1:


Assuming that, for every occurrence of the findword string, the next occurrence of "OWNER:*" is the one to be changed, the following code will hopefully work:

Private Sub PRODA_Replace_IfNDM_Click()
    Dim FindWord As String, Found As Range
    Dim ownerCell As Range
    Dim firstAddress As String
    Dim rslt As Range
    FindWord = "/*_NDM*"
    With Sheets("PRODA")
        Set Found = .Cells.Find(What:=FindWord, _
                                LookIn:=xlValues, _
                                lookAt:=xlPart, _
                                searchOrder:=xlByRows, _
                                searchDirection:=xlNext, _
                                MatchCase:=False)
        If Not Found Is Nothing Then
            firstAddress = Found.Address
            Do
                Set ownerCell = .Cells.Find(What:="OWNER:*", _
                                            After:=Found, _
                                            LookIn:=xlValues, _
                                            lookAt:=xlPart, _
                                            searchOrder:=xlByRows, _
                                            searchDirection:=xlNext, _
                                            MatchCase:=False)
                If ownerCell Is Nothing Then
                    MsgBox "No corresponding owner for word found at " & Found.Address
                    Exit Sub
                End If
                ownerCell.Value = "OWNER:chrndm"
                'Can't do FindNext, because we did a different Find
                Set Found = .Cells.Find(What:=FindWord, _
                                        After:=Found, _
                                        LookIn:=xlValues, _
                                        lookAt:=xlPart, _
                                        searchOrder:=xlByRows, _
                                        searchDirection:=xlNext, _
                                        MatchCase:=False)
                If Found.Address = firstAddress Then
                    Exit Do
                End If
            Loop
            MsgBox "changed successfully"
        End If
    End With
End Sub

I've tested with some dummy data which hopefully replicates what you are doing.



来源:https://stackoverflow.com/questions/41195207/searching-for-multiple-occurrence-of-a-text-and-changing-another-text-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!