Solved

Macro stopped working

Posted on 2015-01-26
4
52 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
ID: 40570321
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
ID: 40570332
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
ID: 40570337
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
ID: 40570344
many thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

840 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