View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
XR8 Sprintless XR8 Sprintless is offline
external usenet poster
 
Posts: 19
Default Find a value in a sheet and replace it with another value ifit matches

On 2/09/2014 9:55 PM, Claus Busch wrote:
Hi

Thanks for your assistance.

try:

Sub ReplaceMatches()

Dim shtOld As Worksheet, shtNew As Worksheet
Dim i As Integer, f As Range
Dim LRowD As Long, LRowE As Long
Dim FirstAddress As String

Set shtOld = ThisWorkbook.Sheets("Errata")
Set shtNew = ThisWorkbook.Sheets("Data")

With shtOld
LRowE = .Cells(Rows.Count, 1).End(xlUp).Row
LRowD = shtNew.Cells(Rows.Count, 4).End(xlUp).Row
For i = 2 To LRowE
Set f = shtNew.Range("D2:D" & LRowD).Find(.Cells(i, 1), _
LookIn:=xlValues, lookat:=xlPart)
If Not f Is Nothing Then
FirstAddress = f.Address
Do
shtNew.Cells(f.Row, 4) = .Cells(i, 3)
Set f = shtNew.Range("D2:D" & LRowD).FindNext(f)
Loop While Not f Is Nothing And f.Address < FirstAddress
End If
Next i
End With
End Sub


It errors at the Loop While Not f is nothing line after replacing the
first value.
Run time error 91 Object variable or with block variable not set?