-
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?
-
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]
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules