简体   繁体   中英

VBA- adding character to .findnext in loop doesn't exit loop

This code let's you change a value in one table, then looks for the original value('oldcode') in another range, if there's one or more occurrences in the 'other range', it replaces all of the oldcode with newcode.

This works perfectly when taking away characters from newcode, however, when adding even one character to newcode, the loop never stops. For example, if current (oldcode) is "Test", and I type "tes", the code fires and all of the "test" are changed to "tes". If I change "Test" to "Test 1", all are changed to "Test 1", but the loop continues to run even though C is nothing after all are changed. The if within the do doesn't seem to help.

I should also mention oldcode is not directly "Test", Oldcode actually comes from column 1, where it concats "test" and counts how many of them there, so "test-1".

Any help would be greatly appreciated!

Private Sub worksheet_change(ByVal target As Range)

Dim row As Integer
Dim column As Integer
Dim i As Integer
Dim oldcode As String
Dim newcode As String
Dim IssueLogSheet As Worksheet
Dim FailureModeTable As Range
Dim max As Integer


Set IssueLogSheet = Sheets("Issue Log")
Set FailureModeTable = IssueLogSheet.Range("FMCODE")

row = target.row
column = target.column



    If Not Intersect(target, FailureModeTable) Is Nothing And (target.column <> 1 Or target.column <> 4) Then


        Application.EnableEvents = False
        Application.Undo
        oldcode = Cells(row, 1).Value
        oldcode = WorksheetFunction.Proper(oldcode)
        Application.Undo
        Application.EnableEvents = True
        MsgBox oldcode


            With IssueLogSheet.Range("IssueLogFailureName")
            Set c = .Find(oldcode, LookIn:=xlValues)

                If Not c Is Nothing Then
                newcode = Cells(row, 1).Value
                newcode = WorksheetFunction.Proper(newcode)


                    Do
                      If c Is Nothing Then
                      Exit do
                      End If
                    c.Value = newcode
                    Set c = .FindNext(c)
                    Loop While Not c Is Nothing


                End If

          End With

      End If

End Sub

add

Dim firstAddress As String

and change the loop as follows:

    With IssueLogSheet.Range("IssueLogFailureName")
        Set c = .Find(oldcode, LookIn:=xlValues)

        If Not c Is Nothing Then
            firstAddress = c.Address '<--| store first occurrence address
            newcode = WorksheetFunction.Proper(Cells(row, 1).Value)
            Do
                c.Value = newcode
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress '<--| exit should 'Find()' wrap back to first occurrence
        End If
    End With

otherwise just change the loop as follows

    With IssueLogSheet.Range("IssueLogFailureName")
        Set c = .Find(oldcode, LookIn:=xlValues, lookat:=xlWhole) '<--| impose a full match

        If Not c Is Nothing Then
            newcode = WorksheetFunction.Proper(Cells(row, 1).Value)
            Do
                c.Value = newcode
                Set c = .FindNext(c)
            Loop While Not c Is Nothing
        End If
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM