0

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?

3
  • Add Option Explicit to the very top of your module and then compile. Commented Jul 28, 2017 at 14:23
  • @braX I already did that and get the same thing. The problem is that sometimes it works fine but sometimes it still get the error without a reason (at least for me). Commented Jul 28, 2017 at 14:24
  • @braX without the error handling the error occurs on line If FoundCell.Address = FirstAddr Or i >= 30 Then Commented Jul 28, 2017 at 14:26

2 Answers 2

1
If Not FoundCell Is Nothing Then
  If FoundCell.Address = FirstAddr Or i >= 30 Then
     Exit Do
  End If
End If
Sign up to request clarification or add additional context in comments.

2 Comments

why If Not FoundCell Is Nothing Then instead of If FoundCell Is Nothing Then?
Because it if is Nothing it will not have an Address property to read.
0

In the code below the FoundCell might be set to nothing (if the findnext returns nothing) before the address is tested, this'll give an error.

    Set FoundCell = Sheets("BD").Columns(5).FindNext(After:=FoundCell)
    If FoundCell.Address = FirstAddr Or i >= 30 Then
        Exit Do
    End If

A test to see if FoundCell is nothing before the address-test might solve this:

Do Until FoundCell Is Nothing
    i = i + 1
    FoundCell.Value = newAloc.Value

    Set FoundCell = Sheets("BD").Columns(5).FindNext(After:=FoundCell)
    If FoundCell is nothing then Exit Do
    If FoundCell.Address = FirstAddr Or i >= 30 Then Exit Do
Loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.