Solved

Macro stopped working

Posted on 2015-01-26
4
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

734 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