Solved

Macro stopped working

Posted on 2015-01-26
4
49 Views
Last Modified: 2015-01-26
I have been using the below Macro [provided by Rgonzo1971] which finds a specific word and then deletes all rows above it, but it has now stopped working with the error Object Variable or With block Variable not set

CountrpartyRow = Cells.Find(What:="Counterparty Code", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Row
    If CountrpartyRow > 1 Then
        Rows("1:" & CountrpartyRow - 1).Delete
    End If

Can an Expert assist please.
0
Comment
Question by:Jagwarman
4 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Does the phrase "Counterparty Code" exist in the spreadsheet AFTER the active cell?

If not, then you need to test for that.
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
Could be because the search term isn't being found.

Has the data changed? Is the code meant to look at a specific sheet?

If you use code like this you can avoid the error.
Dim rngFnd As Range
    
    Set rngFnd = Cells.Find(What:="Counterparty Code", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)
     
    If rngFnd Is Nothing Then
        MsgBox "'Counterparty Code' not found."
    Else
        CountrpartyRow = rngFnd.Row
        If CountrpartyRow > 1 Then
            Rows("1:" & CountrpartyRow - 1).Delete
        End If
    End If

Open in new window

0
 
LVL 11

Expert Comment

by:Wilder1626
Comment Utility
You just need to add Counterparty Code in the spreadsheet after the active cell.

It is probably failing because it is missing. Just tested it and it does not fail when Counterparty Code is showing.
0
 

Author Closing Comment

by:Jagwarman
Comment Utility
many thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now