All the informations about the my sheets are explained HERE. But I'll explain it quickly:
I have 3 sheets (Plan1, BANCO and DB). Plan1 has named ranges where I insert information and this information is stored on BANCO and copied to BD (this last one saves all past informations while BANCO only has last information inserted).
I also have a code to verify if the named range alocacao already exists on BD and if it exists, they're load again on Plan1. After this, you can change the name of alocacao after inserting the new name in a range named substituit_aloc using the code below:
Sub SubstituirProduto_Click()
Dim FoundCell As Range, FirstAddr As String, fnd As String, newAloc As Range, i As Long
On Error GoTo Catch
fnd = Sheets("Plan1").Range("alocacao").Value
Set newAloc = Sheets("Plan1").Range("substituir_aloc")
Set FoundCell = Sheets("BD").Columns(5).Find(what:=fnd, _
After:=Sheets("BD").Cells(Rows.Count, 5), Lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
i = i + 1
FoundCell.Value = newAloc.Value
Set FoundCell = Sheets("BD").Columns(5).FindNext(After:=FoundCell)
If FoundCell.Address = FirstAddr Or i >= 30 Then
Exit Do
End If
Loop
Catch:
MsgBox "Substituido!"
End Sub
Sometimes it works and sometimes it gets:
Run-time error 91: Object variable or With block variable not set
And highlithed line:
If FoundCell.Address = FirstAddr Or i >= 30 Then
Despite the error, it does what it need to without influencing the end result. Thus I added the On Error GoTo Catch just to doesn't show the error message and finishs to run the code, but I'm still getting the error message.
Someone know why it is still showing the error message without being catched by my error handling?
Option Explicitto the very top of your module and then compile.If FoundCell.Address = FirstAddr Or i >= 30 Then