2

I need to reverse engineer someone else's code and make it run. Right now it is throwing an error at me complaining that some variable isn't set.

I'm not sure what it is talking about exactly. Here is the top part of the subroutine with the error.

Sub OBI_Vendor_Detail_Macro()
    Cells.Select
    With Selection
        .WrapText = False
        .MergeCells = False
    End With
    Range("C4").Select
    Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

The last line produces this error Run-time error '91': Object variable or With block variable not set

Hope I've provided enough info. If not please let me know. Thanks.

4
  • 1
    The error you get is because there are no matches of the string you are searching Commented Apr 1, 2017 at 23:47
  • Let me see if I understand, is Find() supposed to return a cell with the string, and call Activate() on it, but since it is not returning a valid cell object it is invalid to call Activate() on it? Commented Apr 1, 2017 at 23:56
  • 1
    Exactly. So you can change that or check if the text you are looking for (in this case "Grand Total") is no longer a searchable variable within the sheet. I would suspect the Information on the WorkSheet changed in some way Commented Apr 2, 2017 at 0:00
  • OK I have it now, that was the right answer. Thanks. Commented Apr 2, 2017 at 0:35

1 Answer 1

2

You need to try to Find first, then check that you found something:

Dim foundCell As Range
Set foundCell = Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If Not foundCell Is Nothing Then
  foundCell.Activate
End If
Sign up to request clarification or add additional context in comments.

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.