Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Macro stopped working

Posted on 2015-01-26
4
Medium Priority
?
57 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

670 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