0

I am receiving an object variable error when I run a Macro.

Object Variable or with block variable not set

This occurs on the second time I run the Macro through. Here is the code that is causing the error:

' Select the first Junk Row and use it to delete all rows afterward
     With ActiveSheet
        LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
    End With

    With Columns("F")
        .Find(What:="Total", After:=.Cells(1, 1), LookIn:=xlValues).Activate - **Error occurs here.**
    End With
    A = ActiveCell.Row
    Range(A & ":" & LastRow).Delete

Any suggestions would be greatly appreciated. If I shut the program down it works fine the next time through.

Thanks, Scott

3
  • Your Find() didn't find a match... Commented Feb 20, 2014 at 0:09
  • Not sure why it didn't I manually went through and saw there was a match. Have since instituted a workaround by using another column however I would like to know why this is occuring. Thanks. Commented Feb 20, 2014 at 0:33
  • Tim is right. also .Find argument LookAt is by default set to xlPart so if there really is Total in there, even if you have trailing or leading spaces, it will find it. Commented Feb 20, 2014 at 1:29

3 Answers 3

1

The first time you run the code it deletes the bottom rows including the row containing "Total". The second time you run the code it fails because the first pass deleted the "Total"

Sign up to request clarification or add additional context in comments.

Comments

0

Before activating the cell . its good to check whether the .find found the cell or not .. Below is the example .

Tested

Sub testing()
Dim rng As Range
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
    End With

    With Columns("F")
      Set rng = .Find(What:="Total", After:=.Cells(1, 1), LookIn:=xlValues) 
    End With

    If Not rng Is Nothing Then

    A = rng.Row

    Range(A & ":" & LastRow).Delete

    Else

    MsgBox ("Total not found")

    End If

End Sub

Comments

0

Try and replace the

.find (...).activate  

by:

.find (...).select

Even if i disagree with the use of select/activate.

simplest was

A= .find (....) .row  
'might return 0 or <nothing> or error , if no match ( .match is faster by the way)

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.