Consulting

Results 1 to 3 of 3

Thread: Solved: Find and Replace in Range - Compare Codes

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    34
    Location

    Solved: Find and Replace in Range - Compare Codes

    Excel help provides this nice example for FindNext to find and replace a value in a specified range. It works nicely until the very last error gave me an error saying the object isn't defined or something on the line

    [VBA]Loop While Not c Is Nothing And c.Address <> firstAddress [/VBA]
    Code:
    [VBA]
    With Worksheets(1).Range("a1:a500")
    Set c = .Find(2, lookin:=xlValues)
    If Not c Is Nothing Then
    firstAddress = c.Address
    Do
    c.Value = 5
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
    End With[/VBA]Since this didn't work and I found it to be extraneous code, I used the following code to fine/replace:

    [VBA]
    For i = 1 To SRStat.Rows.Count
    If SRStat(i) = findtext Then
    SRStat(i) = replacetext
    End If
    Next i
    [/VBA]

    Any comments?

  2. #2
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    You could use the built in Replace (ctrl+h)
    [VBA]Sub findReplace()
    Dim r As Range
    Set r = Range("a1:j100")
    With r
    .Replace what:="a", replacement:="z", lookat:=xlWhole
    End With
    End Sub[/VBA]

  3. #3
    VBAX Regular
    Joined
    Oct 2007
    Posts
    34
    Location
    sweet

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •